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.
First separate colors you use once from colors you use at least twice. A once-used color is the annoying one: in its triple with its two neighbors, those neighbors must be equal.
If every chosen color is used at least twice, life is easy: put equal numbers in blocks. Blocks of length make all boundary triples safe.
A singleton color must look like . Even worse, if there are other colors nearby, it really needs buffer copies: .
For one repeated color with copies, you can insert at most singleton colors. For two or more repeated colors, a color with copies contributes only singleton slots.
Try every suffix as the set of repeated colors. If the suffix starts at , its base value is , and it can add singleton colors. Take the maximum.
Call a color heavy if we use it at least twice, and single if we use it exactly once.
A single color is very constrained. Suppose the single card is , and its neighbors are and . The triple must contain two equal values. Since appears only once, we must have:
So every singleton has to be inserted between two equal cards, like . The circle is not being cute here; it is forcing structure.
If we use only heavy colors, we can arrange them in blocks:
Every block has length at least , so any triple crossing a boundary still contains two equal cards. Therefore all chosen heavy colors can use all their available cards.
Now suppose we have chosen some heavy colors.
If the heavy color has copies, place them in a circle. A singleton can be inserted into a gap between two equal copies.
But two inserted singleton gaps cannot be adjacent, because that would create:
with all three values distinct. So the maximum number of singleton insertions is:
Now each heavy color block has other colors on both sides. If we insert a singleton too close to the edge of a block, we create a bad triple involving the neighboring block.
For a block of equal cards, usable insertion gaps are internal and non-adjacent. The maximum is:
So for several heavy colors, the total number of singleton slots is:
For a fixed number of heavy colors, we should choose the largest counts. Bigger gives more cards and never gives fewer singleton slots. Since the input is sorted, the heavy colors are some suffix.
Suppose the suffix starts at index in -based indexing. Then:
There are remaining colors outside the suffix, and each can contribute at most one singleton card. If the chosen suffix has singleton positions, we can add:
cards.
So the candidate answer is:
Try every valid suffix whose first count is at least .
Finally, if the best value is less than , output , because we must choose at least three cards.
We scan the array once from right to left, maintaining suffix sums and slot counts.
per test case, and
over the whole input. Memory is , mostly because we store the counts.
#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<ll> c(n);
for (ll &x : c) cin >> x;
ll ans = 0;
ll suffixSum = 0;
ll manyHeavySlots = 0;
for (int i = n - 1; i >= 0; --i) {
suffixSum += c[i];
manyHeavySlots += max(0LL, (c[i] - 2) / 2);
if (c[i] < 2) continue;
int heavyCount = n - i;
ll slots;
if (heavyCount == 1) {
slots = c[i] / 2;
} else {
slots = manyHeavySlots;
}
ll singleColors = i;
ll cur = suffixSum + min(singleColors, slots);
ans = max(ans, cur);
}
if (ans < 3) ans = 0;
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<ll> c(n);
for (ll &x : c) cin >> x;
ll ans = 0;
ll suffixSum = 0;
ll manyHeavySlots = 0;
for (int i = n - 1; i >= 0; --i) {
suffixSum += c[i];
manyHeavySlots += max(0LL, (c[i] - 2) / 2);
if (c[i] < 2) continue;
int heavyCount = n - i;
ll slots;
if (heavyCount == 1) {
slots = c[i] / 2;
} else {
slots = manyHeavySlots;
}
ll singleColors = i;
ll cur = suffixSum + min(singleColors, slots);
ans = max(ans, cur);
}
if (ans < 3) ans = 0;
cout << ans << '\n';
}
}