Codeforces Round #420 (Div. 2)

A. Okabe and Future Gadget Laboratory
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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.

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".


题目大意:


判断是否每一个数都存在等式:ax, y = ax, s + at, y,(当ax,y不是1的时候);


思路:


O(n^4)暴力即可。


B. Okabe and Banana Trees
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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.

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.


题目大意:


给你一条直线,然你找和X,Y轴共同组成的矩形的最大价值是多少。

一个矩形的价值是这个矩形中所有的点X和Y的加和。


思路:


O(b)枚举交点坐标y轴,然后算出矩形右上角坐标(x,y);

然后考虑每个点的贡献值即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
    __int64 m,b;
    while(~scanf("%I64d%I64d",&m,&b))
    {
        __int64 output=0;
        for(__int64 y=0;y<=b;y++)
        {
            __int64 x=(b-y)*m;
            __int64 val=((0+x)*(x+1)/2)*(y+1)+(0+y)*(y+1)/2*(x+1);
            output=max(val,output);
        }
        printf("%I64d\n",output);
    }
}


C. Okabe and Boxes
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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.

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.


题目大意:


一共有2*n个操作,要么是往一个栈里放一个数字,或者是移除栈顶元素,现在主人公想要使得移除的元素排列出来是1~N的排列。现在他可以随时将栈内元素排序,问最少排序的次数。


思路:


其实问题就是在问你会出现多少次错误,如果此时栈顶元素和想要得到的序列的下一个元素不同,那么我们就要进行一次排序。

统计错误的次数即可。

出错之后将栈内元素清空,因为其序列已经排序了,无论怎样Remove一定是按照想要得到的序列来的,所以不必要模拟这个过程。

栈模拟。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int output=0;
        int cnt=1;
        stack<int >s;
        for(int i=1;i<=n*2;i++)
        {
            char tmp[150];
            scanf("%s",tmp);
            if(tmp[0]=='a')
            {
                int d;
                scanf("%d",&d);
                s.push(d);
            }
            else
            {
                if(s.size()>0)
                {
                    if(s.top()==cnt)
                    {
                        s.pop();
                    }
                    else
                    {
                        while(!s.empty())s.pop();
                        output++;
                    }
                }
                cnt++;
            }
        }
        printf("%d\n",output);
    }
}


D.最短路套路题。我是萌萌哒D题题解


E.Dp+矩阵快速幂,不是很难想。我是萌萌哒E题题解












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值