1. 程式人生 > >Jon Snow and his Favourite Number CodeForces - 768C (技巧)

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; }