CF - D. AND, OR and square sum
Problem
You are given a collection of non-negative integers.
You can perform following operations:
choose two distinct indices . If before the operation ,, then after the operation ,.
Maximize
Solution
英語書くの疲れたので、日本語でやります。
・ある一つの操作について考える。
一般性を失わず、 とする。
, とすると, ,.
とすると、一度の操作で だけ答えを増加させる事ができる。
・以上の考察より、となるが存在する限りこの操作を行えば良い。
つまり、できるだけ大きい数字のbitを1に変えていけばよい事がわかる。
であり、の bitが0,の bitが1の場合を考える。
操作を行うと、の bitが1,の bitが0になる。この操作を繰り返して、できるだけbitを1にすれば良い。
あとはbitごとに1が立っている数を数えて、できるだけ大きい数を構築していけば良い。
#include<bits/stdc++.h> using Int = int64_t; using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); Int N; cin >> N; vector<Int> A(N); for(auto& e : A) cin >> e; vector pos(20,0); for(auto e : A){ for(int bit = 0; bit < pos.size(); ++bit) if(e & (1 << bit)) pos[bit]++; } vector ans(N,0LL); for(auto& e : ans){ for(int bit = 0; bit < pos.size(); ++bit){ if(pos[bit] > 0 ){ e += (1LL << bit); pos[bit]--; } } } cout << accumulate(ans.begin(),ans.end(),0LL, [](auto res,auto e){ return res += e * e; }) << endl; }