Codeforces #443 (div 2)

来源:互联网 发布:重庆大学数据图书馆 编辑:程序博客网 时间:2024/06/16 20:01

A. Borya’s Diagnosis

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

It seems that Borya is seriously sick. He is going visit n doctors to find out the exact diagnosis. Each of the doctors needs the information about all previous visits, so Borya has to visit them in the prescribed order (i.e. Borya should first visit doctor 1, then doctor 2, then doctor 3 and so on). Borya will get the information about his health from the last doctor.

Doctors have a strange working schedule. The doctor i goes to work on the si-th day and works every di day. So, he works on days si, si + di, si + 2di, ….

The doctor’s appointment takes quite a long time, so Borya can not see more than one doctor per day. What is the minimum time he needs to visit all doctors?

Input
First line contains an integer n — number of doctors (1 ≤ n ≤ 1000).

Next n lines contain two numbers si and di (1 ≤ si, di ≤ 1000).

Output
Output a single integer — the minimum day at which Borya can visit the last doctor.

Examples

input
3
2 2
1 2
2 2

output
4

input
2
10 1
6 5

output
11

Note
In the first sample case, Borya can visit all doctors on days 2, 3 and 4.

In the second sample case, Borya can visit all doctors on days 10 and 11.

题意:第i个人可以走到a_i+k*b_i点,每个人走到的点不能冲突,且走到的点单调上升。问按给定的输入顺序,走的最远的人最近点在哪里。

思路:直接模拟。

#include<cstdio>using namespace std;#define     N       100005int i,j,k,l,n,m,ans,a[N],b[N];int main(){    scanf("%d",&n);    for (i=1;i<=n;i++) scanf("%d%d",&a[i],&b[i]);    ans=a[1];    for (i=2;i<=n;i++)    {        if (a[i]<=ans) a[i]+=(ans-a[i])/b[i]*b[i];        if (a[i]<=ans) a[i]+=b[i];        ans=a[i];    }    printf("%d",ans);    return 0;}

B. Table Tennis

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

n people are standing in a line to play table tennis. At first, the first two players in the line play a game. Then the loser goes to the end of the line, and the winner plays with the next person from the line, and so on. They play until someone wins k games in a row. This player becomes the winner.

For each of the participants, you know the power to play table tennis, and for all players these values are different. In a game the player with greater power always wins. Determine who will be the winner.

Input
The first line contains two integers: n and k (2 ≤ n ≤ 500, 2 ≤ k ≤ 1012) — the number of people and the number of wins after which a player leaves, respectively.

The second line contains n integers a1, a2, …, an (1 ≤ ai ≤ n) — powers of the player. It’s guaranteed that this line contains a valid permutation, i.e. all ai are distinct.

Output
Output a single integer — power of the winner.

Examples

input
2 2
1 2

output
2

input
4 2
3 1 2 4

output
3

input
6 2
6 5 3 1 2 4

output
6

input
2 10000000000
2 1

output
2

Note
Games in the second sample:

3 plays with 1. 3 wins. 1 goes to the end of the line.

3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.

题意:有n个人,每个人有一个实力值a_i,每一轮的前两个人进行战斗,实力小的被排到最后,问第一个赢下m轮的人的实力值。

思路:我们发现当战斗次数超过n时,站在第一个的一定是实力最强的人,因此我们的枚举是有上界的。然后直接模拟即可,注意一个人输的时候胜者胜场数也要+1

#include<cstdio>using namespace std;#define     N       10005int i,j,k,l,n,a[N],t;long long m;int main(){    scanf("%d%I64d",&n,&m);    for (i=1;i<=n;i++) scanf("%d",&a[i]);    for (j=1;j<=1000000;j++)    {        if (a[1]>a[2])        {            t=a[2];            for (i=2;i<n;i++) a[i]=a[i+1];            a[n]=t;            k++;            if ((long long)k>=m)            {                printf("%d",a[1]);                return 0;            }        }        else        {            t=a[1];            for (i=1;i<n;i++) a[i]=a[i+1];            a[n]=t;            k=1;        }    }    printf("%d\n",a[1]);}

C. Short Program

time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPetya learned a new programming language CALPAS. A program in this language always takes one non-negative integer and returns one non-negative integer as well.In the language, there are only three commands: apply a bitwise operation AND, OR or XOR with a given constant to the current integer. A program can contain an arbitrary sequence of these operations with arbitrary constants from 0 to 1023. When the program is run, all operations are applied (in the given order) to the argument and in the end the result integer is returned.Petya wrote a program in this language, but it turned out to be too long. Write a program in CALPAS that does the same thing as the Petya's program, and consists of no more than 5 lines. Your program should return the same integer as Petya's program for all arguments from 0 to 1023.InputThe first line contains an integer n (1 ≤ n ≤ 5·105) — the number of lines.Next n lines contain commands. A command consists of a character that represents the operation ("&", "|" or "^" for AND, OR or XOR respectively), and the constant xi 0 ≤ xi ≤ 1023.OutputOutput an integer k (0 ≤ k ≤ 5) — the length of your program.Next k lines must contain commands in the same format as in the input.Examplesinput3| 3^ 2| 1output2| 3input3& 1& 3output1& 1input3^ 1^ 2^ 3output0NoteYou can read about bitwise operations in https://en.wikipedia.org/wiki/Bitwise_operation.Second sample:Let x be an input of the Petya's program. It's output is ((x&1)&3)&5 = x&(1&3&5) = x&1. So these two programs always give the same outputs.

题意:给你一些位运算操作,求一个长度不长于5的对于0-1023都成立的等价操作序列

思路:由于他只要求0-1023,我们可以对10位二进制位分别进行操作。对于每一位的运算之后我们能找到一个能够拟合的操作方式,直接模拟即可

#include<cstdio>   using namespace std;#define     N       500005int i,j,k,l,n,m,z1,z2,p,q,t,ans1,ans2,ans3;char s[N][5];int x[N];int main(){    scanf("%d",&n);    for (i=1;i<=n;i++)    {        scanf("%s%d",&s[i],&x[i]);    }    for (i=0,q=1;i<=9;i++,q<<=1)    {        z1=0;        for (j=1;j<=n;j++)        {            t=0;if (x[j]&q) t=1;            if (s[j][0]=='&') z1&=t;            if (s[j][0]=='|') z1|=t;            if (s[j][0]=='^') z1^=t;        }        t=0;z2=1;        for (j=1;j<=n;j++)        {            t=0;if (x[j]&q) t=1;            if (s[j][0]=='&') z2&=t;            if (s[j][0]=='|') z2|=t;            if (s[j][0]=='^') z2^=t;        }        if (z1==0&&z2==0)        {            ans2+=q;            ans3+=q;        }        if (z1==1&&z2==0)        {            ans3+=q;        }        if (z1==1&&z2==1)        {            ans2+=q;        }    }    if (ans1) p++;    if (ans2) p++;    if (ans3) p++;    printf("%d\n",p);    if (ans1) printf("& %d\n",ans1);    if (ans2) printf("| %d\n",ans2);    if (ans3) printf("^ %d\n",ans3);}
原创粉丝点击