题解+补题

比赛链接:https://codeforces.com/contest/879

题目:A. Borya's Diagnosis

样例1:

输入:

3
2 2
1 2
2 2

输出

4

解释:这个题就是模拟这个人看医生的过程就行,因为题目中的数据太小,直接暴力就能过

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;

typedef pair<int,int> PII;

PII p[N];
int n;
int main()
{
    cin >> n;
    for(int i = 0;i < n;i ++)
        cin >> p[i].first >> p[i].second;
    
    int start = p[0].first;
    
    for(int i = 1;i < n;i ++)
    {
        int day = p[i].first;
        while(1)
        {
            if(day > start){
                start = day;
                break;
            }else{
                day = day + p[i].second;
            }
        }
    }
    
    cout << start << endl;
    
    return 0;
}

题目:B. Lucky Mask

样例1:

输入:

2 2
1 2

输出

2 

解释:能够连续的PK掉K个人的那个人获胜,输出那个人的战力值

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;
typedef long long LL;
typedef pair<int,int> PII;
LL k;
int n;
int a[N];
int main()
{
    cin >> n >> k;
    
    if(k >= n - 1){
        cout << n << endl;
        return 0;
    }
    
    for(int i = 0;i < n;i ++)
        cin >> a[i];
    
    bool tf = false;
    
    while(1)
    {
        int cnt = 0;
        queue<int>q;
        int i;
        for(i = 1;i < n;i ++)
        {
            if(a[0] > a[i]){
                q.push(a[i]);
                cnt ++;
            }else{
                break;
            }
            if(cnt >= k){
                cout << a[0] << endl;
                return 0;
            }
        }
        int j = 0;
        int aim = a[0];
        for(j = 0;i < n;j ++,i ++)
            a[j] = a[i];
        
        a[j ++] = aim;
        for(j;j < n;j ++)
        {
            a[j] = q.front();
            q.pop();
        }
        
        if(!tf){
            k --;
            tf = true;
        }
        
    }
    return 0;
}

题目:C. Short Program

样例1:

输入:

3
| 3
^ 2
| 1


输出

2
| 3
^ 2

解释:答案不唯一,我想的是先与、再或、再异或,把X分解成10位,然后分别讨论每一位是0还是1的情况,再把每一位经过若干次操作后,最终得到0还是1,然后再分4种情况讨论,
即0-0,0-1,1-0,1-1

#include <bits/stdc++.h>
using namespace std;

const int N = 5e5 + 10;
typedef long long LL;
typedef pair<char,int> PII;
int n;

PII p[N];
int b[N];
int a[N];

int main()
{
    cin >> n;
    
    for(int i = 0;i < n;i ++)
        cin >> p[i].first >> p[i].second;
    
    for(int i = 0;i < 10;i ++)
    {
        a[i] = 0;
        for(int j = 0;j < n;j ++)
        {
            if(p[j].first == '|'){
                a[i] = a[i] | ((p[j].second >> i)& 1);
            }else if(p[j].first == '&'){
                a[i] = a[i] & ((p[j].second >> i)& 1);
            }
            else{
                a[i] = a[i] ^ ((p[j].second >> i)& 1);
            }

        }
    }
    
    
    
    for(int i = 0;i < 10;i ++)
    {
        b[i] = 1;
        for(int j = 0;j < n;j ++)
        {
            if(p[j].first == '|'){
                b[i] = b[i] | (p[j].second >> i & 1);
            }else if(p[j].first == '&'){
                b[i] = b[i] & (p[j].second >> i & 1);
            }
            else{
                b[i] = b[i] ^ (p[j].second >> i & 1);
            }
        }
    }
    
    int f1 = 0,f2 = 0,f3 = 0;
    
    for(int i = 9;i >= 0;i --)
    {
        if(a[i] == 0){
            if(b[i] == 0){
                 
            }else{
                f1 = f1 + (1 << i); // 与
            }
        }else{
            if(b[i] == 1){
                f2 = f2 + (1 << i); // 或
            }else{
                f1 = f1 + (1 << i);// 与
                f3 = f3 + (1 << i); // 异或
            }
        }
    }
    
    cout << 3 << endl;
    cout << "& " << f1 << endl;
    cout << "| " << f2 << endl;
    cout << "^ " << f3 << endl;
    
    
  /*  cout << endl;
    
    int x,y;
    cin >> x;
    y = x;
    x = x & f1;
    x = x | f2;
    x = x ^ f3;
    
    y = y | 999;
    y = y ^ 689;
    
    cout << x  << ' ' << y << endl;*/
    
    return 0;
}
知足常乐!
原文地址:https://www.cnblogs.com/yjsh/p/14327791.html