https://atcoder.jp/contests/arc135/tasks/arc135_c #includeusing namespace std; #define rep(i,l,r) for(int i=(l);i<=(r);i++) #define per(i,l,r) for(int i=(l);i>=(r);i--) #define ll long long #define pii pair #define mset(s,t) memset(s,t,sizeof(t)) #define mcpy(s,t) memcpy(s,t,sizeof(t)) #define fir first #define pb push_back #define sec second #define sortall(x) sort((x).begin(),(x).end()) inline int read () { int x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f |= (ch=='-'),ch= getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return f?-x:x; } template void print(T x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) print(x/10); putchar(x % 10 + '0'); } const int N = 35; int cnt[N]; int a[300005]; int n; void solve() { cin >> n;ll ans = 0; for (int i = 1; i <= n; i ++) { cin >> a[i]; int t= a[i]; ans += t; for (int j =0; j <= 30; j ++) cnt[j] += (t >> j & 1); } for (int i = 1; i <= n; i++) { ll res = 0; for (int j = 0; j <= 30; j++) res += ((a[i] >> j & 1) == 1? n - cnt[j] : cnt[j]) * ((ll)1 << j); ans = max (ans, res); } cout << ans << endl; } int main () { int t; t =1; while (t --) solve(); return 0; }
这题难就难在怎么高效率的统计异或之后的总和。我们通过观察二进制,对于每一个选择的值,我们课已发现,如果当前值的二进制位是0,那么所有其他数的相同位置的二进制为上的1会被保留,并且贡献等于1的个数乘该位置的权值。否则就是算0的个数的贡献。注意要考虑初始状态的综合。我一直在想怎么求和,没有把数字当作二进制位来考虑,所以无法找到规律。下次遇到这种题目应该考虑二进制表示。
异或的一些性质 对于数列A, B,B是从A异或操作后得到的结果。 a[i]^a[j] = b[i]^b[j];



