Jon Snow and his Favourite Number CodeForces - 768C (技巧)

链接

题意

给定数组, 每次操作先将数组排序, 再将奇数位全部异或x, 求k次操作后数组最大值与最小值

(1 ≤ n ≤ 1050 ≤ k ≤ 1050 ≤ x ≤ 103)

题解

直接暴力模拟是O(nk)的, 注意到元素范围均较小可以用桶达到O(1024*k)


该题k过大时会好像一定出现循环, 然后暴力模拟几次也能过, 没看出来怎么证明...

#include <iostream>
#include <algorithm>
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std;

const int N = 1e5+10;
int n, k, x, t;
int a[N], delta[N];

int main() {
    cin>>n>>k>>x;
    REP(i,1,n) cin>>t, ++a[t];
    REP(i,1,k) {
        REP(i,0,1023) delta[i] = 0;
        int cur = 1;
        REP(i,0,1023) {
            int cnt = (a[i]+cur)/2;
            delta[i] -= cnt;
            delta[i^x] += cnt;
            cur ^= a[i]&1;
        }
        REP(i,0,1023) a[i] += delta[i];
    }
    int mi = 1e9, ma = 0;
    REP(i,0,1023) if (a[i]) {
        mi = min(mi, i);
        ma = max(ma, i);
    }
    cout<<ma<<' '<<mi<<endl;
}

猜你喜欢

转载自www.cnblogs.com/uid001/p/10225723.html