C: DRM Messages
题目描述
DRM Encryption is a new kind of encryption. Given an encrypted string (which we’ll call a DRM message),the decryption process involves three steps: Divide, Rotate and Merge. This process is described in the following example with the DRM message “EWPGAJRB”:
Divide – First, divide the message in half to “EWPG” and “AJRB”.
Rotate – For each half, calculate its rotation value by summing up the values of each character (A = 0, B =1, . . . Z = 25). The rotation value of “EWPG” is 4 + 22 + 15 + 6 = 47. Rotate each character in “EWPG” 47 positions forward (wrapping from Z to A when necessary) to obtain the new string “ZRKB”. Following the same process on “AJRB” results in “BKSC”.
Merge – The last step is to combine these new strings (“ZRKB” and “BKSC”) by rotating each character in the first string by the value of the corresponding character in the second string. For the first position, rotating ‘Z’ by ‘B’ means moving it forward 1 character, which wraps it around to ‘A’. Continuing this process for every character results in the final decrypted message, “ABCD”.
Divide – First, divide the message in half to “EWPG” and “AJRB”.
Rotate – For each half, calculate its rotation value by summing up the values of each character (A = 0, B =1, . . . Z = 25). The rotation value of “EWPG” is 4 + 22 + 15 + 6 = 47. Rotate each character in “EWPG” 47 positions forward (wrapping from Z to A when necessary) to obtain the new string “ZRKB”. Following the same process on “AJRB” results in “BKSC”.
Merge – The last step is to combine these new strings (“ZRKB” and “BKSC”) by rotating each character in the first string by the value of the corresponding character in the second string. For the first position, rotating ‘Z’ by ‘B’ means moving it forward 1 character, which wraps it around to ‘A’. Continuing this process for every character results in the final decrypted message, “ABCD”.
输入
The input contains a single DRM message to be decrypted. All characters in the string are uppercase letters and the string’s length is even and ≤ 15 000.
输出
Display the decrypted DRM message.
样例输入
EWPGAJRB
样例输出
ABCD
#include <bits/stdc++.h> using namespace std; string s; int a[15005],b[15005]; int main() { cin>>s; int len=s.size(); int a1=0,a2=0; for(int i=0;i<len/2;i++) a1+=(s[i]-'A'); for(int i=len/2;i<len;i++) a2+=(s[i]-'A'); for(int i=0;i<len/2;i++) a[i]=((s[i]-'A')+a1)%26; for(int i=len/2;i<len;i++) b[i-len/2]=((s[i]-'A')+a2)%26; for(int i=0;i<len/2;i++) a[i]=(a[i]+b[i])%26; string ans=""; for(int i=0;i<len/2;i++) ans+=(a[i]+'A'); cout<<ans<<endl; return 0; }
问题 D: Game of Throwns
题目描述
Daenerys frequently invents games to help teach her second grade Computer Science class about various aspects of the discipline. For this week’s lesson she has the children form a circle and (carefully) throw around a petrified dragon egg.
The n children are numbered from 0 to n − 1 (it is a Computer Science class after all) clockwise around the circle. Child 0 always starts with the egg. Daenerys will call out one of two things:
1. a number t, indicating that the egg is to be thrown to the child who is t positions clockwise from the current egg holder, wrapping around if necessary. If t is negative, then the throw is to the counter-clockwise direction.
2. the phrase undo m, indicating that the last m throws should be undone. Note that undo commands never undo other undo commands; they just undo commands described in item 1 above.
For example, if there are 5 children, and the teacher calls out the four throw commands 8 -2 3 undo 2, the throws will start from child 0 to child 3, then from child 3 to child 1, then from child 1 to child 4. After this, the undo 2 instructions will result in the egg being thrown back from child 4 to child 1 and then from child 1 back to child 3. If Daenerys calls out 0 (or n, −n, 2n, −2n, etc.) then the child with the egg simply throws it straight up in the air and (carefully) catches it again.
Daenerys would like a little program that determines where the egg should end up if her commands are executed correctly. Don’t ask what happens to the children if this isn’t the case.
The n children are numbered from 0 to n − 1 (it is a Computer Science class after all) clockwise around the circle. Child 0 always starts with the egg. Daenerys will call out one of two things:
1. a number t, indicating that the egg is to be thrown to the child who is t positions clockwise from the current egg holder, wrapping around if necessary. If t is negative, then the throw is to the counter-clockwise direction.
2. the phrase undo m, indicating that the last m throws should be undone. Note that undo commands never undo other undo commands; they just undo commands described in item 1 above.
For example, if there are 5 children, and the teacher calls out the four throw commands 8 -2 3 undo 2, the throws will start from child 0 to child 3, then from child 3 to child 1, then from child 1 to child 4. After this, the undo 2 instructions will result in the egg being thrown back from child 4 to child 1 and then from child 1 back to child 3. If Daenerys calls out 0 (or n, −n, 2n, −2n, etc.) then the child with the egg simply throws it straight up in the air and (carefully) catches it again.
Daenerys would like a little program that determines where the egg should end up if her commands are executed correctly. Don’t ask what happens to the children if this isn’t the case.
输入
Input consists of two lines. The first line contains two positive integers n k (1 ≤ n ≤ 30, 1 ≤ k ≤ 100) indicating the number of students and how many throw commands Daenerys calls out, respectively. The following line contains the k throw commands. Each command is either an integer p (−10 000 ≤ p ≤ 10 000) indicating how many positions to throw the egg clockwise or undo m (m ≥ 1) indicating that the last m throws should be undone. Daenerys never has the kids undo beyond the start of the game.
输出
Display the number of the child with the egg at the end of the game.
样例输入
5 4
8 -2 3 undo 2
样例输出
3
#include <bits/stdc++.h> using namespace std; struct node { int id,num; }mp[200]; string s; int n,k; int a[200]; int fun(string t) { int ans=0; if(t[0]=='-') { int k=10; for(int i=1;i<t.size();i++) { ans=ans*k+(t[i]-'0'); // k*=10; } ans=-ans; } else { int k=10; for(int i=0;i<t.size();i++) { ans=ans*k+(t[i]-'0'); // k*=10; } } return ans; } stack<int>q; int main() { cin>>n>>k; for(int i=0;i<k;i++) { cin>>s; if(s[0]=='u') { mp[i].id=2; cin>>mp[i].num; } else { mp[i].id=1; mp[i].num=fun(s); } } int now=0; q.push(now); for(int i=0;i<k;i++) { if(mp[i].id==1) { now+=mp[i].num; while(now<0) { now+=3000*n; } now%=n; q.push(now); } else { for(int j=0;j<mp[i].num;j++) q.pop(); now=q.top(); } } cout<<now<<endl; return 0; }
问题 G: A Question of Ingestion
题目描述
Stan Ford is a typical college graduate student, meaning that one of the most important things on his mind is where his next meal will be. Fortune has smiled on him as he’s been invited to a multi-course barbecue put on by some of the corporate sponsors of his research team, where each course lasts exactly one hour.Stan is a bit of an analytical type and has determined that his eating pattern over a set of consecutive hours is always very consistent. In the first hour, he can eat up to m calories (where m depends on factors such as stress, bio-rhythms, position of the planets, etc.), but that amount goes down by a factor of two-thirds each consecutive hour afterwards (always truncating in cases of fractions of a calorie). However, if he stops eating for one hour, the next hour he can eat at the same rate as he did before he stopped. So, for example, if m = 900 and he ate for five consecutive hours, the most he could eat each of those hours would be 900, 600, 400, 266 and 177 calories, respectively. If, however, he didn’t eat in the third hour, he could then eat 900, 600, 0, 600 and 400 calories in each of those hours. Furthermore, if Stan can refrain from eating for two hours, then the hour after that he’s capable of eating m calories again. In the example above, if Stan didn’t eat during the third and fourth hours, then he could consume 900, 600, 0, 0 and 900 calories.
Stan is waiting to hear what will be served each hour of the barbecue as he realizes that the menu will determine when and how often he should refrain from eating. For example, if the barbecue lasts 5 hours and the courses served each hour have calories 800, 700, 400, 300, 200 then the best strategy when m = 900 is to eat every hour for a total consumption of 800 + 600 + 400 + 266 + 177 = 2 243 calories. If however, the third course is reduced from 400 calories to 40 calories (some low-calorie celery dish), then the best strategy is to not eat during the third hour — this results in a total consumption of 1 900 calories. The prospect of all this upcoming food has got Stan so frazzled he can’t think straight. Given the number of courses and the number of calories for each course, can you determine the maximum amount of calories Stan can eat?
Stan is waiting to hear what will be served each hour of the barbecue as he realizes that the menu will determine when and how often he should refrain from eating. For example, if the barbecue lasts 5 hours and the courses served each hour have calories 800, 700, 400, 300, 200 then the best strategy when m = 900 is to eat every hour for a total consumption of 800 + 600 + 400 + 266 + 177 = 2 243 calories. If however, the third course is reduced from 400 calories to 40 calories (some low-calorie celery dish), then the best strategy is to not eat during the third hour — this results in a total consumption of 1 900 calories. The prospect of all this upcoming food has got Stan so frazzled he can’t think straight. Given the number of courses and the number of calories for each course, can you determine the maximum amount of calories Stan can eat?
输入
Input starts with a line containing two positive integers n m (n ≤ 100, m ≤ 20 000) indicating the number of courses and the number of calories Stan can eat in the first hour, respectively. The next line contains n positive integers indicating the number of calories for each course.
输出
Display the maximum number of calories Stan can consume.
扫描二维码关注公众号,回复:
3044807 查看本文章
![](/qrcode.jpg)
样例输入
5 900
800 700 400 300 200
样例输出
2243
#include <bits/stdc++.h> using namespace std; int dp[105][105][2]; int main() { int n,maxp; scanf("%d%d",&n,&maxp); int a[105]; for(int i=1; i<=n; i++) scanf("%d",&a[i]); int ap[105]; ap[1] = maxp; for(int i=2; i<=n; i++) ap[i] = ap[i-1]*2/3; // dp[1][1][1] = a[1]>ap[1]?ap[1]:a[1]; // dp[1][0][0] = 0; for(int i=1; i<=n; i++) { dp[i][1][1]=min(a[i],ap[1]); if(i>2) { for(int j=0;j<=i-3;j++) { dp[i][1][1]=max(dp[i][1][1],dp[i-3][j][1]+min(a[i],ap[1])); dp[i][1][1]=max(dp[i][1][1],dp[i-3][j][0]+min(a[i],ap[1])); } } if(i>1) { for(int j=0; j<=i-2; j++) { dp[i][0][0]=max(dp[i][0][0],dp[i-2][j][0]); dp[i][0][0]=max(dp[i][0][0],dp[i-2][j][1]); } } for(int j=1; j<=i; j++) { dp[i][j][1]=max(dp[i][j][1],dp[i-1][j-1][1]+min(a[i],ap[j])); dp[i][j][1]=max(dp[i][j][1],dp[i-1][j][1]); if(i>1) { dp[i][j][1]=max(dp[i][j][1],dp[i-2][j][1]+min(a[i],ap[j])); } } } int ans=0; for(int i=1; i<=n; i++) { for(int j=1; j<=i; j++) { for(int k=0; k<2; k++) { ans=max(ans,dp[i][j][k]); } } } printf("%d\n",ans); return 0; }
H: Sheba’s Amoebas
After a successful Kickstarter campaign, Sheba Arriba has raised enough money for her mail-order biology supply company. “Sheba’s Amoebas” can ship Petri dishes already populated with a colony of those tiny one-celled organisms. However, Sheba needs to be able to verify the number of amoebas her company sends out. For each dish she has a black-and-white image that has been pre-processed to show each amoeba as a simple closed loop of black pixels. (A loop is a minimal set of black pixels in which each pixel is adjacent to exactly two other pixels in the set; adjacent means sharing an edge or corner of a pixel.) All black pixels in the image belong to some loop.
Sheba would like you to write a program that counts the closed loops in a rectangular array of black and white pixels. No two closed loops in the image touch or overlap. One particularly nasty species of cannibalistic amoeba is known to surround and engulf its neighbors; consequently there may be amoebas within amoebas. For instance, each of the images in Figure H.1 contains four amoebas.
Sheba would like you to write a program that counts the closed loops in a rectangular array of black and white pixels. No two closed loops in the image touch or overlap. One particularly nasty species of cannibalistic amoeba is known to surround and engulf its neighbors; consequently there may be amoebas within amoebas. For instance, each of the images in Figure H.1 contains four amoebas.
![](http://exam.upc.edu.cn/upload/image/20171129/20171129081539_53960.jpg)
输入
The first line of input contains two integers m and n, (1 ≤ m, n ≤ 100). This is followed by m lines, each containing n characters. A ‘#’ denotes a black pixel, a ‘.’ denotes a white pixel. For every black pixel, exactly two of its eight neighbors are also black.
输出
Display a single integer representing the number of loops in the input.
样例输入
12 12
.##########.
#..........#
#..#...##..#
#.##..#..#.#
#......#.#.#
#....#..#..#
#...#.#....#
#..#...#...#
.#..#.#....#
#....#.....#
#.........#.
.#########..
样例输出
4
#include <bits/stdc++.h> using namespace std; char Map[105][105]; int dx[8]={0,0,-1,1,-1,1,1,-1}; int dy[8]={1,-1,0,0,1,1,-1,-1}; int n,m; int vis[105][105]; void f(int x,int y) { int nowx; int nowy; for(int i=0;i<8;i++) { nowx = x+dx[i]; nowy = y+dy[i]; if(nowx>=0&&nowx<n&&nowy>=0&&nowy<m&&vis[nowx][nowy]==0&&Map[nowx][nowy]=='#') { vis[nowx][nowy] = 1; Map[nowx][nowy] = '.'; f(nowx,nowy); } } } int main() { memset(vis,0,sizeof(vis)); scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%s",Map[i]); int ans = 0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(Map[i][j]=='#'&&vis[i][j]==0) { vis[i][j] = 1; ans++; f(i,j); } } } cout<<ans<<endl; }