Codeforces Round #443 (Div. 2) A、B、C 位运算

A

题目链接

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.


 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
int main(){
    int n,i,j,s,d, now = 0;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d%d",&s,&d);
        if(now<s){
            now = s;
        }else{
            for(j=0;;j++){
                if(s + d*j > now){
                    break;
                }
            }
            now = s + d*j;
        }
    }
    printf("%d\n",now);

    return 0;
}

B

题目链接

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.

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <deque>
using namespace std;
const int maxn = 510;
const int inf = 0x3f3f3f3f;
int a[maxn];
long long win[maxn];
deque<int> que;
int main(){
    long long n,k,i,j;
    scanf("%lld%lld",&n,&k);
    for(i=1;i<=n;i++){
        scanf("%d",&a[i]);
        que.push_back(a[i]);
    }
    memset(win, 0, sizeof(win));
    bool flag = false;
    while(que.front() != n){
        int a = que.front();
        que.pop_front();
        int b = que.front();
        que.pop_front();
        if(a>b){
            que.push_front(a);
            que.push_back(b);
            ++win[a];
            if(win[a] >= k){
                flag = true;
                printf("%d\n", a);
                break;
            }
        }else{
            que.push_front(b);
            que.push_back(a);
            ++win[b];
            if(win[b] >= k){
                flag = true;
                printf("%d\n", b);
                break;
            }
        }
    }
    if(!flag){
        printf("%lld\n",n);
    }

    return 0;
}

C

C. Short Program
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

题意:给我们一堆位操作,要求输出不超过五行的位操作(仅包含and or xor),使得我们经过我们处理的位操作和经过输入的位操作结果一样。上述操作数均不超过1023
思路:(感谢gungunda讲解)
由于是位操作,那么只要考虑每一个位上的变化即可。因此我们拿0(每一位都是0)和1023(每一位都是1)来做输入的一大堆操作。得到两个数,叫他两 zero , ten 吧。从zero的每一位上可以看到如果原数该位是0的变化,从ten的每一位上可看若原数该位是1的变化。

操作10
^101
^010
|111
|010
&110
&000

根据上表,可以看出来实际只需要两个操作 or 和 xor 就行了(因为操作数为1的程序好些啊啊啊啊),当:
0>0,1>1 啥也用干
0>1,1>1 做个 or 1
0>1,1>0 做个xor 1
0>0,1>0 做个or 再加个xor

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
int status[20][2];
char op;
int main(){
    int n,i,j,x;
    scanf("%d",&n);
    int zero = 0, ten = 1023;
    for(i=1;i<=n;i++){
        getchar();
        scanf("%c %d", &op, &x);
        if(op=='|'){
            zero |= x;
            ten |= x;
        }else if(op=='^'){
            zero ^= x;
            ten ^= x;
        }else{
            zero &= x;
            ten &= x;
        }
    }
    int a1 = 0,a2 = 0,a3 = 0;
    // and or xor
    for(i=0;i<10;i++){
        int now = (1<<i);
        if( (zero&now) && (ten&now) ){
            a2 += now;
        }else if( (zero&now) && !(ten&now) ){
            a3 += now;
        }else if( !(zero&now) && !(ten&now) ){
            a2 += now;
            a3 += now;
        }
    }
    printf("2\n");
    printf("| %d\n^ %d\n",a2,a3);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值