【LDU】2018个人PK赛#3

A - Okabe and Future Gadget Laboratory 

Codeforces Round #420 (Div. 2)

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


这道题其实看例子就能容易看出是什么,就是有一个二维数组。
里面的每一个元素都可以在   该元素的同行同列中找到  两个数相加得到该元素,
如果它是1,则不考虑。
就如题意上写的是   a x,y  = a x,s+a t,y  ; 
要是有一个不合法就输出NO。合法就输出Yes。
不想太多直接暴力解决

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main()
{
    int n;
    scanf("%d",&n);
    ll a[55][55];
    int flag=1;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            scanf("%lld",&a[i][j]);
        }
    }
    int x,y;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(a[i][j]==1)
                continue;
            for(x=0;x<n;x++)
            {
                for(y=0;y<n;y++)
                {
                    if(a[i][j]==a[x][j]+a[i][y])
                           break;
                }
                if(y!=n)
                    break;
            }
            if(x==n)
                flag=0;
        }
    }
    if(flag)
    {
        printf("YES\n");
    }
    else
    {
        printf("NO\n");
    }
    return 0;
}


B - Okabe and Banana Trees 

Codeforces Round #420 (Div. 2)

这个题我之前做过,它就是用一个等差数列来做,

首先,因为它一定是过坐标  (X,Y) 的,且(x与y都是整数)

大家看式子,式中是  y=-x/m+b;   只要我确保y是整数,x肯定是整数。枚举y的所有情况。

问题又来了,我一开始说是用等差数列,至于怎么用呢。

其实就是把y所在的枚举同时,内设一个循环来计算,

该循环是用  X代表在一条线上的所有x1+x2+x3....xn的值

而用 j×(x+1),j代表的是y值,(x+1)是个数。


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int m,b;
    scanf("%d%d",&m,&b);
    ll maxz=0;
    for(int y=0;y<=b;y++)
    {
        ll x=(b-y)*m;
        ll X=((x)*(x+1))/2;
        ll sum=0;
        for(int j=0;j<=y;j++)
        {
            sum+=j*(x+1)+X;
        }
        maxz=max(sum,maxz);
    }
    printf("%lld\n",maxz);
    return 0;
}




C - Okabe and Boxes 

Codeforces Round #420 (Div. 2)

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.

说一下这道题大意吧,就是OK设置里一个一系列指令来刁难DA,使得从数组出来时的数字是按1~n的顺序,但是DA很厉害,在数组里面的时候可以更换顺序。问题是问要更换多少次。其实这题就是模拟栈的问题。那就用栈来实现。以下代码可能有些是看了别人网上的做法做的,如有侵犯,敬请原谅。


#include<stdio.h>
#include<stack>
using namespace std;
stack<int>st;
char op[10];
int main()
{
    int n,x,ans,cur;
    scanf("%d",&n);
   // getchar();
    cur=0;
    ans=0;
    for(int i=0;i<2*n;i++)
    {
        scanf("%s",op);
        if(op[0]=='a')
        {
            scanf("%d",&x);
            st.push(x);
        }
        else
        {
            cur++;
            if(!st.empty()&&st.top()==cur)
            {
                st.pop();
            }
            else if(!st.empty())
            {
                    ans++;
                    while(!st.empty())
                        st.pop();
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}


D - Before an Exam

 


Tomorrow Peter has a Biology exam. He does not like this subject much, but d days ago he learnt that he would have to take this exam. Peter's strict parents made him prepare for the exam immediately, for this purpose he has to study not less than minTimei and not more than maxTimei hours per each i-th day. Moreover, they warned Peter that a day before the exam they would check how he has followed their instructions.

So, today is the day when Peter's parents ask him to show the timetable of his preparatory studies. But the boy has counted only the sum of hours sumTime spent him on preparation, and now he wants to know if he can show his parents a timetable sсhedule with d numbers, where each number sсhedulei stands for the time in hours spent by Peter each i-th day on biology studies, and satisfying the limitations imposed by his parents, and at the same time the sum total of all schedulei should equal to sumTime.

Input

The first input line contains two integer numbers d, sumTime (1 ≤ d ≤ 30, 0 ≤ sumTime ≤ 240) — the amount of days, during which Peter studied, and the total amount of hours, spent on preparation. Each of the following dlines contains two integer numbers minTimei, maxTimei (0 ≤ minTimei ≤ maxTimei ≤ 8), separated by a space — minimum and maximum amount of hours that Peter could spent in the i-th day.

Output

In the first line print YES, and in the second line print d numbers (separated by a space), each of the numbers — amount of hours, spent by Peter on preparation in the corresponding day, if he followed his parents' instructions; or print NO in the unique line. If there are many solutions, print any of them.


题意很简单就是说,有个同学,它不喜欢学生物,导致要爸妈监督下学习,爸妈每天安排了规定的时间内复习,

但是同学只记得自己曾经复习的总时间,但是不知道每天复习的时间,要你去给出每天的时间。

还有一个不合法性,要是最少的总和比它大不行,要是最大的总和比它小也不行。

这题我的做法是先把全部时间设置为最大值,然后在把差值给一个一个减去,才得到最小值。

#include<bits/stdc++.h>
using namespace std;
typedef struct point
{
    int x,y,index,output,diff;
} point;
int cmp1(point a, point b)
{
    return a.diff>b.diff;
}
int cmp2(point a,point b)
{
    return a.index<b.index;
}
int main()
{
    int d,sumtime;
    while(~scanf("%d%d",&d,&sumtime))
    {
        int flag=1;
        point a[35];
        int sum1=0,sum2=0;
        for(int i=0; i<d; i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
            a[i].index=i;
            a[i].diff=a[i].y-a[i].x;
            sum1+=a[i].x;
            sum2+=a[i].y;
        }
        if(sum2<sumtime||sum1>sumtime)
        {
            printf("NO\n");
            flag=0;
        }
        else
        {
            printf("YES\n");
        }
        if(flag)
        {
            sum2-=sumtime;
            sort(a,a+d,cmp1);
        for(int i=0; i<d; i++)
        {
            if(sum2-a[i].diff>0)
            {
                sum2-=a[i].diff;
                a[i].output=a[i].x;
            }
            else if(sum2-a[i].diff<=0)
            {
                a[i].output=a[i].y-sum2;
                sum2=0;
            }
            else if(sum2==0)
            {
                a[i].output=a[i].y;
            }
        }
            sort(a,a+d,cmp2);
            for(int i=0; i<d; i++)
            {
                printf(i==0?"%d":" %d",a[i].output);
            }
            printf("\n");
        }
    }
    return 0;
}

E - Registration system 

A new e-mail service "Berlandesk" is going to be opened in Berland in the near future. The site administration wants to launch their project as soon as possible, that's why they ask you to help. You're suggested to implement the prototype of site registration system. The system should work on the following principle.

Each time a new user wants to register, he sends to the system a request with his name. If such a name does not exist in the system database, it is inserted into the database, and the user gets the response OK, confirming the successful registration. If the name already exists in the system database, the system makes up a new user name, sends it to the user as a prompt and also inserts the prompt into the database. The new name is formed by the following rule. Numbers, starting with 1, are appended one after another to name (name1name2, ...), among these numbers the least i is found so that namei does not yet exist in the database.

Input

The first line contains number n (1 ≤ n ≤ 105). The following n lines contain the requests to the system. Each request is a non-empty line, and consists of not more than 32 characters, which are all lowercase Latin letters.

Output

Print n lines, which are system responses to the requests: OK in case of successful registration, or a prompt with a new name, if the requested name is already taken.

Examples
input
4
abacaba
acaba
abacaba
acab
output
OK
OK
abacaba1
OK
input
6
first
first
second
second
third
third
output
OK
first1
OK
second1
OK
third1


这题其实就是用户名重复就在其后加数字。

利用的map容器的做法进行标记。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    map<int,string>my;
    map<string,int>vis;
    int b[100005];
    char a[100005][33];
    scanf("%d",&n);
    getchar();
    queue<string>q;
    for(int i=0;i<n;i++)
    {
        string a;
        cin>>a;
        q.push(a);
        vis[a]=0;
        my[i]=a;
    }
    int cnt=0;
    while(!q.empty())
    {
        string s;
        s=q.front();
        q.pop();
        if(vis[s]==0)
        {
            printf("OK\n");
            vis[s]++;
        }
        else
        {
            vis[s]++;
            cout<<s;
            printf("%d\n",vis[s]-1);
        }
        cnt++;
    }
    return 0;
}


F - Robot Motion

 



A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are 

N north (up the page)
S south (down the page)
E east (to the right on the page)
W west (to the left on the page)

For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid.

Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.

You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.
 

Input
There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.
 

Output
For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1.
 

Sample Input
 
 
3 6 5NEESWEWWWESSSNWWWW4 5 1SESWEEESNWNWEENEWSEN0 0
 

Sample Output
 
 
10 step(s) to exit3 step(s) before a loop of 8 step(s)


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dir[4][2]={
    {-1,0}, //N
    {0,1},  //E
    {1,0},  //S
    {0,-1}  //W
};
int n,m,s;
int vis[1000][1000];
char G[1000][1000];
int main()
{
    while(scanf("%d%d%d",&n,&m,&s),(n||m||s))
    {
        getchar();
        memset(vis,-1,sizeof(vis));
        memset(G,0,sizeof(G));
        for(int i=0;i<n;i++)
        {
            scanf("%s",G[i]);
            getchar();
        }
        int flag=1;
        int x=0,y=s-1;
        int step=0;
        while(flag)
        {
            if(x<0||y<0||x>=n||y>=m)
            {
                printf("%d step(s) to exit\n",step);
                flag=0;
                break;
            }
            if(vis[x][y]!=-1)
            {
                printf("%d step(s) before a loop of %d step(s)\n",vis[x][y],step-vis[x][y]);
                flag=0;
                break;
            }
            vis[x][y]=step;
            step++;
            switch(G[x][y])
            {
                case 'N':x+=dir[0][0], y+=dir[0][1];break;
                case 'E':x+=dir[1][0], y+=dir[1][1];break;
                case 'S':x+=dir[2][0], y+=dir[2][1];break;
                case 'W':x+=dir[3][0], y+=dir[3][1];break;
            }
        }
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值