Problem: MAXAND18.
I got 50 points for this problem. I’m very surprised because I got WA in the first subtask (k \leq 2) but AC in the second subtask (k \leq 30). How is this possible?
Here is my code. Please give me a testcase for which my program fails (or let me know the issue).
#include <bits/stdc++.h>
using namespace std;
bool sortbysec(const pair<long long, long long> &a, const pair<long long, long long> &b) {return (a.second < b.second);}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--)
{
int n, k;
cin >> n >> k;
vector <int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
vector <pair<long long, long long>> bits(30, {0, 0});
for (int i = 0; i < 30; ++i)
{
long long bs = 0;
for (int j = 0; j < n; ++j) if (a[j] & (1LL << i)) bs += (1LL << i);
bits[i].first = bs, bits[i].second = i;
}
sort(bits.rbegin(), bits.rend());
int prev = -1;
vector <pair <long long, long long>> temp(0), res(0);
for (int i = 0; i < 30; ++i)
{
if (prev == -1)
{
prev = bits[i].first;
temp.push_back(bits[i]);
} else {
if (bits[i].first == prev) temp.push_back(bits[i]);
else {
prev = -1;
sort(temp.begin(), temp.end(), sortbysec);
for (int i = 0; i < temp.size(); ++i) res.push_back(temp[i]);
temp.clear();
--i;
}
}
if (i == 29)
{
sort(temp.begin(), temp.end(), sortbysec);
for (int i = 0; i < temp.size(); ++i) res.push_back(temp[i]);
temp.clear();
prev = -1;
}
}
long long ans = 0;
for (int i = 0; i < k; ++i) ans += (1LL << res[i].second);
cout << ans << '\n';
}
}
First I find out the contribution of each bit for all numbers. I store that sum along with the bit in a pair. I sort the vector of pairs so that I can get the maximum sum in the beginning. What if multiple sums are equal? I choose the one having the smallest bit (smallest x is to be chosen among all x for which S is maximum). I sort the vectors with the same sum accordingly. I just append that vector to the resultant vector. I iterate over the first k elements of the vector adding 2^{\text{number of bit}} each time.
Please help me fix this issue.
