A - A
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
3
2 2
1 2
2 2
4
2
10 1
6 5
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.
思路:一开始读这道题的时候理解错了,受底下帮助解题的惯性思维的错误引导以为拜访医生的时间不仅每天至多一次而且还需要每天的日期成等差数列排列,然后思路上就绕了很大一圈,最后想了半天,才看清题意,要求每天至多拜访一次,而且一定是先拜访第1个医生,再是第2个、第3个...求最早哪天拜访完所有医生。
理解清楚题意就很好解题了,我的思路是直接模拟,将最小值设置为第一个医生的初始值,
然后在输入的同时每读入一组数据,判断当前初始值(记为a)与最小值(记为c)的大小关系,
若a>c,则将最小值赋值为a,可以保证拜访的日期不重复,实时更新数据;
若a<=c,将a不断加b,直至a>c。
代码:
#include<iostream> using namespace std; int main(){ int n,a,b,i,j,c=0; cin>>n; for(i=0;i<n;i++){ cin>>a>>b; if(c<a){ c=a; } else if(c>=a){ while(c>=a){ //牢记while循环的判断条件为真是才执行 a=a+b; } c=a; } } cout<<c<<endl; return 0; }
B - B
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.
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
2 2
1 2
2
4 2
3 1 2 4
3
6 2
6 5 3 1 2 4
6
2 10000000000
2 1
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人站成一排打乒乓球,队伍最前面2个进行力量大小的pk,力量大者胜出,然后输者走到队伍的末尾,赢家和队伍中的下一个人继续玩,以此类推,直到有人连续赢了k场,输出这个玩家的力量值。
一开始我采取的是两次遍历,可想而知时间复杂度太大没有ac,但是仔细想想,其实只要遍历队列一次即可,赢家一定存在这个队伍里,这时考虑到可能原队列最后一名玩家是赢家的情况,其实也只要一次遍历正常结束即可,那么将初始值(b)设置为a[0],每次与它进行比较,大就总计(sum)加1,小就将当前a[i]赋值给b,要注意此时的sum值为1!!!因为它已经进行过一次比较且胜出,以此类推,最后输出b值即可。
代码:
#include<iostream> using namespace std; int main(){ long long int n,k,i,a[1000],b,sum=0;//注意k的取值范围 cin>>n>>k; for(i=0;i<n;i++){ cin>>a[i]; } b=a[0];//将初始值赋值为a[0] for(i=1;i<n;i++){//遍历完结束 if(b>a[i]){ sum++; } else{ b=a[i]; sum=1;//此时b<=a[i],已经存在一次 } if(sum==k){ break;//总数达到k结束 } } cout<<b<<endl; return 0; }
C - C
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
3
| 3
^ 2
| 1
2
| 3
^ 2
3
& 1
& 3
& 5
1
& 1
3
^ 1
^ 2
^ 3
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.
思路:题意说任找出5组以内的操作,使其结果等于这n种操作的结果。我一开始想的是只要输出&、|、^这三种运算即可,然后就将所有相同的运算的数字进行该操作再输出相应的位运算和该数字即可。但是在test7 wa掉了;
看了其他博客学到了,分别用全1和全0的数进行一遍输入的操作,比较结果中的各个位,如果都是0,则都是0,如果都是1,则都是1,如果原为0后为1原为1后为0则异或,和原来不变则没有操作。
附一个关于位运算的博客,比较详细https://www.cnblogs.com/yrjns/p/11246163.html
#include<iostream> #include<cstring> using namespace std; int main(){ int n,i,a,x=0,y=1023; string s; cin>>n; for(i=0;i<n;i++){ cin>>s>>a; if(s=="&"){ x=x&a; y=y&a; } else if(s=="|"){ x=x|a; y=y|a; } else if(s=="^"){ x=x^a; y=y^a; } } int aand=0,oor=0,xxor=0; aand=x|y;//可以与上b二进制表示中1的部分 oor=x&y;//0->1,1->1,两个二进制中都是1的部分 xxor=x&(y^1023);//0->1,1->0,两个二进制中都变成1的部分 cout<<3<<endl; cout<<"& "<<aand<<endl; cout<<"| "<<oor<<endl; cout<<"^ "<<xxor<<endl; return 0; }