[关闭]
@zzzc18 2017-06-27T13:21:32.000000Z 字数 5154 阅读 1499

Codeforces Round #420 (Div. 2)

Codeforces


Problem A Okabe and Future Gadget Laboratory

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an n by n square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In other words, for every x, y such that 1 ≤ x, y ≤ n and ax, y ≠ 1, there should exist two indices s and t so that ax, y = ax, s + at, y, where ai, j denotes the integer in i-th row and j-th column.

Help Okabe determine whether a given lab is good!

Input
The first line of input contains the integer n (1 ≤ n ≤ 50) — the size of the lab.

The next n lines contain n space-separated integers denoting a row of the grid. The j-th integer in the i-th row is ai, j (1 ≤ ai, j ≤ 105).

Output
Print "Yes" if the given lab is good and "No" otherwise.

You can output each letter in upper or lower case.

Samples
input
3
1 1 2
2 3 1
6 4 1
output
Yes
input
3
1 5 2
1 1 1
1 2 3
output
No

  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. int n;
  5. int a[60][60];
  6. bool check(int x,int y){
  7. int num=a[x][y];
  8. int i,j;
  9. for(i=1;i<=n;i++){
  10. for(j=1;j<=n;j++){
  11. if(a[i][y]+a[x][j]==num)
  12. return true;
  13. }
  14. }
  15. return false;
  16. }
  17. void solve(){
  18. int cnt=0;
  19. int i,j;
  20. for(i=1;i<=n;i++){
  21. for(j=1;j<=n;j++){
  22. if(a[i][j]==1)
  23. cnt++;
  24. else{
  25. if(check(i,j))
  26. cnt++;
  27. else
  28. break;
  29. }
  30. }
  31. }
  32. if(cnt==n*n)
  33. printf("Yes\n");
  34. else
  35. printf("No\n");
  36. }
  37. int main(){
  38. scanf("%d",&n);
  39. int i,j;
  40. for(i=1;i<=n;i++)
  41. for(j=1;j<=n;j++)
  42. scanf("%d",&a[i][j]);
  43. solve();
  44. return 0;
  45. }

Problem B Okabe and Banana Trees

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

Okabe needs bananas for one of his experiments for some strange reason. So he decides to go to the forest and cut banana trees.

Consider the point (x, y) in the 2D plane such that x and y are integers and 0 ≤ x, y. There is a tree in such a point, and it has x + y bananas. There are no trees nor bananas in other points. Now, Okabe draws a line with equation . Okabe can select a single rectangle with axis aligned sides with all points on or under the line and cut all the trees in all points that are inside or on the border of this rectangle and take their bananas. Okabe's rectangle can be degenerate; that is, it can be a line segment or even a point.

Help Okabe and find the maximum number of bananas he can get if he chooses the rectangle wisely.

Okabe is sure that the answer does not exceed 1018. You can trust him.

Input
The first line of input contains two space-separated integers m and b (1 ≤ m ≤ 1000, 1 ≤ b ≤ 10000).

Output
Print the maximum number of bananas Okabe can get from the trees he cuts.

Samples
input
1 5
output
30
input
2 3
output
25

简单说一句,只要知道右上角坐标(x,y),且由题意,只考虑x,y均为整数的情况,矩形内有多少香蕉可以推算

  1. #include<cstdio>
  2. #include<algorithm>
  3. #define LL long long
  4. using namespace std;
  5. LL m,b;
  6. LL ans;
  7. LL cal(LL x){
  8. return x*(x+1)/2;
  9. }
  10. void solve(){
  11. LL x,y;LL t;
  12. for(y=0;y<=b;y++){
  13. x=b*m-m*y;
  14. LL i;
  15. t=cal(x);
  16. LL add=t;
  17. for(i=1;i<=y;i++){
  18. t+=add+(x+1);
  19. add+=(x+1);
  20. }
  21. ans=max(ans,t);
  22. }
  23. printf("%I64d\n",ans);
  24. }
  25. int main(){
  26. scanf("%I64d%I64d",&m,&b);
  27. solve();
  28. return 0;
  29. }

Problem C Okabe and Boxes

time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

Okabe and Super Hacker Daru are stacking and removing boxes. There are n boxes numbered from 1 to n. Initially there are no boxes on the stack.

Okabe, being a control freak, gives Daru 2n commands: n of which are to add a box to the top of the stack, and n of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to n. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack.

That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it.

Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed.

Input
The first line of input contains the integer n (1 ≤ n ≤ 3·105) — the number of boxes.

Each of the next 2n lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer x (1 ≤ x ≤ n) follows, indicating that Daru should add the box with number x to the top of the stack.

It is guaranteed that exactly n lines contain "add" operations, all the boxes added are distinct, and n lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed.

Output
Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands.

Samples
input
3
add 1
remove
add 2
add 3
remove
remove
output
1
input
7
add 3
add 2
add 1
remove
add 4
remove
remove
remove
add 6
add 7
add 5
remove
remove
remove
output
2

Note
In the first sample, Daru should reorder the boxes after adding box 3 to the stack.

In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack.

Solution

用一个栈来储存add的内容,每次remove时//你应把以下几点一块看了,虽然有先后
1.如果栈为空,本次remove就不需要我们做什么,continue;
2.检查栈顶元素是不是需要的元素(第i次remove就该是i)如果是,弹出,否则转3
3.将所有栈中元素弹出,表示我们排过序了(这时应该就明白1了,因为题目说remove前一定有所需的add)

  1. #include<cstdio>
  2. #include<stack>
  3. #include<algorithm>
  4. using namespace std;
  5. int n;
  6. int ans,cnt;
  7. char s[20];
  8. stack<int> sa;
  9. int main(){
  10. scanf("%d",&n);
  11. int i;
  12. for(i=1;i<=2*n;i++){
  13. scanf("%s",s);
  14. if(s[0]=='a'){
  15. int x;
  16. scanf("%d",&x);
  17. sa.push(x);
  18. }
  19. else{
  20. cnt++;
  21. if(!sa.empty()){//if is empty means all elements are already sorted
  22. if(sa.top()!=cnt){
  23. ans++;
  24. while(!sa.empty()) sa.pop();
  25. }
  26. else{
  27. sa.pop();
  28. }
  29. }
  30. }
  31. }
  32. printf("%d\n",ans);
  33. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注