【CodeForces

来源:互联网 发布:淘宝尺寸勾选 编辑:程序博客网 时间:2024/06/07 04:17

A - Borya’s Diagnosis
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.

Example
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.
题意: n个医生,每个医生有个第一次上班的时间s,之后每隔d天上班一次,现在想要从1到n顺序的访问医生,问最小的天数。
分析: 水题,模拟就可以。

#include<bits/stdc++.h>using namespace std;typedef pair<int,int>pii;#define first fi#define second se#define  LL long long#define fread() freopen("in.txt","r",stdin)#define fwrite() freopen("out.txt","w",stdout)#define CLOSE() ios_base::sync_with_stdio(false)const int MAXN = 1e5;const int MAXM = 1e6;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;double a[MAXN],d[MAXN];int main(){    CLOSE();//  fread();//  fwrite();    int n;cin>>n;    for(int i=1;i<=n;i++){        cin>>a[i];  cin>>d[i];    }    LL time=(LL)a[1];    for(int i=2;i<=n;i++){        if(a[i]<=time){            int ge=ceil(((double)time-a[i])/d[i]); // 找到第一次大于上次访问时间            if(a[i]+ge*d[i]==time) ge++;            time=a[i]+ge*d[i];        }else time=(LL)a[i];    }    printf("%lld\n",time);    return 0;}

B - Table Tennin 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.

Example
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个人站成一队,每个人都有不同的 战斗力,每次两个人比赛,赢的人留下继续比赛,输的人退到队伍最后,这样不断循环,问谁先得到k次胜利。
分析:水题,我们只需要遍历一遍队列就行了,如果遍历一遍还没有人到k次,那么之后赢的人肯定一直是战斗力最高的人。
代码

#include<bits/stdc++.h>using namespace std;typedef pair<int,int>pii;#define first fi#define second se#define  LL long long#define fread() freopen("in.txt","r",stdin)#define fwrite() freopen("out.txt","w",stdout)#define CLOSE() ios_base::sync_with_stdio(false)const int MAXN = 1e5;const int MAXM = 1e6;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;LL power[MAXN];int vis[MAXN];int main(){    CLOSE();//  fread();//  fwrite();     LL n,k;cin>>n>>k;     for(int i=1;i<=n;i++)  cin>>power[i];        LL waite=power[1];        for(int i=2;i<=n;i++){            if(power[i]>waite){                 vis[power[i]]++;                 if(vis[power[i]]==k) {                    printf("%lld\n",power[i]);                    return 0;                  }                 waite=power[i];             }else {                vis[waite]++;                 if(vis[waite]==k) {                    printf("%lld\n",waite);                    return 0;                  }             }         }         printf("%lld\n",waite);    return 0;}

C - Short Program
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.

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

题意:假设有个函数可能会输入0-1023的数字,现在题目输入函数内容,对传进来的参数进行操作,并且返回相应的值,但是我们现在嫌弃其函数内容写的太长了,让我们构造一个函数内容,5行之内,对所有可能传进来的参数,返回的值和原来的一样。

分析: 想了半天也不知道题目让干嘛。看了别人的代码,才懂。
其实就是构造。
0-1023其实就是9位二进制数,我们先预处理出来每一二进制位的情况-> 即输入的函数参数 当前二进制位可能0或者1,把经过n次处理之后的值分别存起来。
然后就是构造
这里写图片描述
别问我为什么这么构造(不断假设然后修改使其满足情况就行)。
有点难说清,详细的看代码

#include<bits/stdc++.h>using namespace std;typedef pair<int,int>pii;#define first fi#define second se#define  LL long long#define fread() freopen("in.txt","r",stdin)#define fwrite() freopen("out.txt","w",stdout)#define CLOSE() ios_base::sync_with_stdio(false)const int MAXN = 5e5+11;const int MAXM = 1e6;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;char op[MAXN];int a[MAXN];int F[10][2],f[10][3];int main(){    CLOSE();//  fread();//  fwrite();    int n;scanf("%d",&n);    for(int i=1;i<=n;i++){        char ch[5];        scanf("%s%d",ch,&a[i]);        op[i]=ch[0];    }    for(int i=0;i<10;i++){        int x=1<<i; int y=0<<i;  // 枚举的二进制的位        for(int j=1;j<=n;j++){            if(op[j]=='&') {                x=x&a[j];                y=y&a[j];            }else if(op[j]=='|') {                x=x|a[j];                y=y|a[j];            }else if(op[j]=='^'){                x=x^a[j];                y=y^a[j];            }        }        x>>=i; y>>=i; //回到最后一位,要判断最后变成了什么        F[i][1]=x&1; F[i][0]=y&1;  // &1 获取最后一位的值    //  printf("%d %d\n",x,y);    }    for(int i=0;i<10;i++){        if(F[i][1]&&!F[i][0]) {            f[i][0]=1;            f[i][1]=0;            f[i][2]=0;        }else if(F[i][1]&&F[i][0]) {            f[i][0]=1;            f[i][1]=1;            f[i][2]=0;        }else if(!F[i][1]&&F[i][0]) {            f[i][0]=1;            f[i][1]=0;            f[i][2]=1;        }else if(!F[i][1]&&!F[i][0]) {            f[i][0]=0;            f[i][1]=0;            f[i][2]=0;        }    }    int x=0,y=0,z=0;    for(int i=9;i>=0;i--){ // 获得三种处理的构造值        x=(x<<1)+f[i][0];        y=(y<<1)+f[i][1];        z=(z<<1)+f[i][2];    }    printf("3\n& %d\n| %d\n^ %d\n",x,y,z);    return 0;}
原创粉丝点击