Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Ignore the order. Since only cares about frequencies, splitting into subsequences is really just choosing how many copies of each value go into ; gets the rest.
For a value with total count , suppose gets copies. Its contribution is whether is odd plus whether is odd.
If is odd, then and have opposite parity, so this value always contributes exactly . No choice matters for it.
If is even, then and have the same parity. This value contributes if is odd, and if is even.
Let be the number of odd-frequency values and the number of positive even-frequency values. If , parity can be adjusted, so use all . If , the number of used even-frequency values must have the same parity as .
The word subsequence is mostly decoration here. Since only cares about occurrence counts, the order of elements does not matter. We are just deciding, for each distinct value, how many copies go into . Then gets the rest.
Contribution of one value
Fix some value with total count . Suppose gets copies of it, so gets copies.
This value contributes:
Now split by the parity of .
If is odd, then and have opposite parity. Exactly one of them is odd, always. So every odd-frequency value contributes exactly , no matter how we split it. There is nothing to optimize there.
If is even, then and have the same parity. So this value contributes:
So even-frequency values are the only interesting ones. For each such value, we want to put an odd number of copies into .
Let:
The part is fixed. The only question is: how many of the values can we make odd in ?
When there is at least one odd-frequency value
If , then parity is flexible. An odd-frequency value can be split with either parity on , and it still contributes exactly either way. So it can absorb the annoying parity issue.
That means we can make every even-frequency value good: give each one an odd count in . Since each such value appears at least twice, giving it one copy is valid, and then extra copies can be added in pairs without changing parity. The odd-frequency values handle whatever leftover parity is needed.
So if , the answer is:
.
When all frequencies are even
Now suppose . Every value has even total count.
If we make values good, then exactly those values have odd counts in . All other values have even counts in . Therefore the parity of is the same as the parity of .
But , so we must have:
.
We want the largest possible with that parity:
Then the answer is .
This is the whole trick. The problem looks like it wants a complicated construction, but the output is only the maximum value, so parity does basically all the work. Classic Codeforces: count stuff, stare at parity, win.
Algorithm
For each test case:
Complexity is per test case, because values are between and .
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> cnt(2 * n + 1, 0);
for (int i = 0; i < 2 * n; i++) {
int x;
cin >> x;
cnt[x]++;
}
int odd = 0, even = 0;
for (int x = 1; x <= 2 * n; x++) {
if (cnt[x] == 0) continue;
if (cnt[x] & 1) odd++;
else even++;
}
int ans;
if (odd > 0) {
ans = odd + 2 * even;
} else {
int good = even;
if ((good & 1) != (n & 1)) good--;
ans = 2 * good;
}
cout << ans << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> cnt(2 * n + 1, 0);
for (int i = 0; i < 2 * n; i++) {
int x;
cin >> x;
cnt[x]++;
}
int odd = 0, even = 0;
for (int x = 1; x <= 2 * n; x++) {
if (cnt[x] == 0) continue;
if (cnt[x] & 1) odd++;
else even++;
}
int ans;
if (odd > 0) {
ans = odd + 2 * even;
} else {
int good = even;
if ((good & 1) != (n & 1)) good--;
ans = 2 * good;
}
cout << ans << '\n';
}
}