CodeForces - 768C Jon Snow and his Favourite Number 桶排

https://vjudge.net/problem/CodeForces-768C

题意:n个数,k次操作,x。每次操作先排序,再让奇数位置上的数据a[i]:=a[i] XOR x;   k<1e5,x<1e5,a[i]<1e3.

题解:由于每个数据为1~1000,且每次操作先排序,所以可以用桶排序维护所有数据。然后模拟操作(我自己模拟的一直wa,换了另一种才ac)。

  网上另外也有人k%=64 然后暴力ac了,还有找循环节的也ac 了。

坑:第一次看codeforce 的数据,结果output answer看反了,用错误数据调试。

  ^x的过程中可能产生0,所以每个循环i=0开始。

  i=maxn;while(--i)if(a[i]){cout<<i;break;}这句代码如果最大值是0不会输出。

  做了好久。。。

 

 

ac代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn = 1025;
int a[maxn],a1[maxn];

int main() {
    
    int n, k, x;
    cin >> n >> k >> x;
    int q;
    for (int i = 1; i <= n; i++)scanf("%d", &q), a[q]++;
    
    while (k--) {
        bool odd = 1;
        memset(a1, 0, sizeof(a1));
        /*for (int i = 1; i <= maxn; i++) if (a[i]) {
            //if(a[i]==1)i
            int t;
            if (a[i] % 2) {
                if (odd)
                     t= (a[i] + 1) / 2, a[i] = (a[i] - t), a1[i^x]+=t;
                else t = a[i] / 2, a[i] = (a[i] - t), a1[i^x]+=t;
                odd = !odd;
            }
            else {
                a[i] = a[i] / 2; a1[i^x] += a[i];
            }
        }
        for (int i = 1; i <= maxn; i++) a[i] += a1[i];

    }*/
        int sum = 0;
        for (int i = 0; i <= maxn; i++) {
            if (sum & 1) {
                a1[i] += (a[i] + 1) >> 1;
                a1[i^x] += a[i] >> 1;
            }
            else {
                a1[i] += a[i] >> 1;
                a1[i^x] += (a[i] + 1) >> 1;
            }
            sum += a[i];
            
        }
        for (int i = 0; i <= maxn; i++) a[i] = a1[i];
    }
    int i = maxn;
    for (int i = maxn; i >= 0; i--) if (a[i]) {
        cout << i << ' ';
        break;
    }
    for (int i = 0; i <= maxn; i++) if(a[i]){
        cout << i;
        break;
    }
    cin >> n;
/*    cout << endl;
    for (int i = 1; i <= maxn; i++) if (a[i]) {
        //while(a[i]--)cout << i<<' ';
        cout << a[i] << '*' << i << ' ';
        //break;
    }
    cin >> n;*/
}

 

posted @ 2018-03-06 20:47  SuuTTT  阅读(238)  评论(0编辑  收藏  举报