Codeforces Round #420 (Div. 2)

Codeforces Round #420 (Div. 2)

Table of Contents A. Okabe and Future Gadget LaboratoryB. Okabe and Banana TreesC. Okabe and Boxes

A. Okabe and Future Gadget Laboratory

Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an n by nsquare 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 a**i, 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.

Examples

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

Note

In the first sample test, the 6 in the bottom left corner is valid because it is the sum of the 2 above it and the 4 on the right. The same holds for every number not equal to 1 in this table, so the answer is "Yes".

In the second sample test, the 5 cannot be formed as the sum of an integer in the same row and an integer in the same column. Thus the answer is "No".

题意:判断一个正方形,是否每一个a_{i,j}!=1 且=存在同一行的一个数+同一列的一个数

Source Code:

#include <bits/stdc++.h>

using namespace std;

int a[55][55];
int n;

bool calc(int x,int y) {

    for(int j=0; j<n; j++) {
        for(int i=0; i<n; i++) {
            if(i==x&&j==y) continue;
            if(a[x][j]+a[i][y]==a[x][y]) return true;
        }
    }
    return false;
}

int main() {

    scanf("%d",&n);

    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
            scanf("%d",&a[i][j]);


    bool flag = true;
    for(int i=0; i<n; i++) {
        for(int j=0; j<n; j++) {
            if(a[i][j]!=1) {
                if(!calc(i,j)) {
                    flag = false;
                    break;
                }
            }
        }
        if(!flag) break;
    }

    if(flag)
        puts("Yes");
    else puts("No");

    return 0;
}

B. Okabe and Banana Trees

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 + ybananas. 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.

Examples

input

1 5

output

30

input

2 3

output

25

Note

The graph above corresponds to sample test 1. The optimal rectangle is shown in red and has 30 bananas.

题意:给定一条直线方程,求一个矩形区域中所有的点p(x,y) ,sum_p^s(Px_i+Py_i) 最大;

分析:根据纵坐标的范围,可以直接枚举高度,求每一个矩形的值,这里的矩形的值,可以用等差数列求和;

#include <bits/stdc++.h>

using namespace std;


int m,b;

long long calc(int h) {

    long long x = (b-h)*m;

    long long sum = 0;
    sum = x*(x+1)/2;

    long long tmp = sum;
    for(int i=0;i<h;i++) {
        sum += (tmp+(x+1));
        tmp = tmp + (x+1);
    }

    return sum;
}

int main()
{

    scanf("%d%d",&m,&b);

    long long ans =  -1;
    for(int i=0;i<b;i++) {
        long long tmp = calc(i);
        ans = max(ans,tmp);
    }

    printf("%I64d
",ans);

    return 0;
}

C. Okabe and Boxes

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.

Examples

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.

题意:给定一个出入栈的顺序,每次可以调整栈里面的数,使得出栈的时候,这些数是1~n的有序序列;求最少调整几次

分析:就拿第一个案例来说,不存在这样的案例,

add 1

remove

add 3

remove

这样无论怎么调整都不可能有序;

也就是说,栈里面的元素一定有当前要出栈的元素;所以,可以每次记录要出栈的数,如果栈顶元素不是要出栈的元素,那么就要进行一次调整,调整到出栈有序;如果栈为空,则说明当前要出栈的元素可以进过我之前的排序而顺利出栈;

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    scanf("%d",&n);

    char cmd[10];
    stack<int> s;
    int cnt = 1;
    int ans = 0;
    for(int i=0; i<2*n; i++)
    {
        scanf("%s",cmd);
        if(cmd[0]=='a')
        {
            int x;
            scanf("%d",&x);
            s.push(x);
        }
        else
        {
            if(s.empty()) {
                cnt++;
                continue;
            }
            else if(!s.empty()&&s.top()==cnt)
            {
                s.pop();
            }
            else
            {
                ans++;
                while(!s.empty())
                    s.pop();
            }
            cnt++;
        }
    }

    printf("%d
",ans);

    return 0;
}
原文地址:https://www.cnblogs.com/TreeDream/p/7088723.html