Codeforces Round #443 (Div. 2) A-C 题解

来源:互联网 发布:sql查询sequence 编辑:程序博客网 时间:2024/06/07 15:17

好久不写博客,把最近的cf题解补回来。

A. Borya's Diagnosis
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 dayssi, 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
32 21 22 2
output
4
input
210 16 5
output
11
Note

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

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


cf的题就是英语难懂,这次的题还好些,毕竟看示例几乎就能看出题意。第一个题就是医生上班的时间类似于等差数列,要求严格按照输入顺序来对数据进行模拟,就是简单的模拟而已。

代码实现:

#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<queue>#include<set>#include<cstdio>#define ll long long#define mset(a,x) memset(a,x,sizeof(a))using namespace std;const double PI=acos(-1);const int inf=0x3f3f3f3f;const double esp=1e-6;const int maxn=1e6+5;const int mod=1e9+7;int dir[4][2]={0,1,1,0,0,-1,-1,0};ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}ll lcm(ll a,ll b){return a/gcd(a,b)*b;}ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}struct node{int s,d;}p[10000];int main(){int n,i,j,k;while(cin>>n){for(i=0;i<n;i++)cin>>p[i].s>>p[i].d;int ans=p[0].s;for(i=1;i<n;i++){while(p[i].s<=ans)p[i].s+=p[i].d;ans=p[i].s;}cout<<ans<<endl;}return 0;}

B. Table Tennis
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 ≤ 5002 ≤ k ≤ 1012) — the number of people and the number of wins.

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 21 2
output
2 
input
4 23 1 2 4
output
3 
input
6 26 5 3 1 2 4
output
6 
input
2 100000000002 1
output
2
Note

Games in the second sample:

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

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


又是一道模拟题,题意是有n个人,每个人都有一个能力值,从第一个人开始,依次跟后面的人进行比赛,输了的去队伍的最尾端,问第一个能连胜k场的人是哪个。

k<=10^12,其实也没那么可怕嘛,n最大不才500,只要k大于等于n输出的一定是最大的那个数,因为只有它才可以连续赢,当k小于n时,模拟就ok了,设置一个计数器从0开始计数,如果它大于前者,达到k-1就可以了。

代码实现:

#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<queue>#include<set>#include<cstdio>#define ll long long#define mset(a,x) memset(a,x,sizeof(a))using namespace std;const double PI=acos(-1);const int inf=0x3f3f3f3f;const double esp=1e-6;const int maxn=1e6+5;const int mod=1e9+7;int dir[4][2]={0,1,1,0,0,-1,-1,0};ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}ll lcm(ll a,ll b){return a/gcd(a,b)*b;}ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}int map[505];int main(){ll n,k,i,j,ans;while(cin>>n>>k){for(i=0;i<n;i++)cin>>map[i];if(k>=n){sort(map,map+n);cout<<map[n-1]<<endl;continue;}int flag=1;for(i=0;i<n&&flag;i++){if(map[i]<k)continue;int sum=0;if(i&&map[i-1]<map[i])sum=1;for(j=1;j<=k-sum;j++){int temp=(i+j)%n;if(map[i]<map[temp])break;if(j==k-sum){flag=0;ans=map[i];break;}}}cout<<ans<<endl;}return 0;}

C. Short Program
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

Input

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

Output

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

Examples
input
3| 3^ 2| 1
output
2| 3^ 2
input
3& 1& 3& 5
output
1& 1
input
3^ 1^ 2^ 3
output
0
Note

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


c题的收获最大,题意说任找出5组以内的操作,使其结果等于n种操作的结果。
大神一句话解决这个题,分别用全1和全0的数进行一遍输入的操作,比较结果中的各个位,如果都是0,则都是0,如果都是1,则都是1,如果原为0后为1原为1后为0则异或,和原来不变则没有操作。
也就是只要是三个& | ^ 操作总能得到结果,然后这个结论我不是很懂,先记住吧。弄懂了再来强行解释一波。
代码实现:
#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<queue>#include<set>#include<cstdio>#define ll long long#define mset(a,x) memset(a,x,sizeof(a))using namespace std;const double PI=acos(-1);const int inf=0x3f3f3f3f;const double esp=1e-6;const int maxn=1e6+5;const int mod=1e9+7;int dir[4][2]={0,1,1,0,0,-1,-1,0};ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}ll lcm(ll a,ll b){return a/gcd(a,b)*b;}ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}int main(){int n,x,i,j,k;char ch;while(cin>>n){int a=0,b=1023;for(i=0;i<n;i++){cin>>ch>>x;if(ch=='&'){a&=x;b&=x;}else if(ch=='|'){a|=x;b|=x;}else{a^=x;b^=x;}}int temp1=a&b;int temp2=a|b;int ans=0;for(i=0;i<10;i++){if((a>>i&1)&&!(b>>i&1))ans|=1<<i; }cout<<"3"<<endl;cout<<"| "<<temp1<<endl;cout<<"& "<<temp2<<endl;cout<<"^ "<<ans<<endl;}return 0;}


原创粉丝点击