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 of the operations for a moment. A card only cares how many total removals happened from the top and how many happened from the bottom.
Let be the number of 0s, the number of 1s, and the number of 2s. No matter what, at least cards vanish from the top and at least cards vanish from the bottom.
Each 2 is just one extra removal that can be assigned to either side. So the final number removed from the top can be any value from to .
Cards are definitely gone, and cards are definitely gone. Cards strictly between and are definitely still there.
Everything else is uncertain: it lies in the zone that some choices of 2 can remove and other choices can spare. Build the answer by starting with all ?, then overwrite guaranteed - and guaranteed + ranges.
Let:
0 operations1 operations2 operationsA 0 always removes from the top. Since the deck starts as , after all operations, at least the first cards must be removed.
A 1 always removes from the bottom, so at least the last cards must be removed.
The only annoying part is 2, but it is not actually that annoying. Each 2 can be treated as choosing either:
So if of the 2 operations are used on the top, then:
and since the other go to the bottom:
where .
That means the number of cards removed from the top can be any value from to . The exact order of operations does not change this. Removing from ends is boring in a useful way: only the final counts matter.
Guaranteed removed cards
Cards through are removed no matter what, because even with every 2 sent to the bottom, the forced top removals still happen.
Cards through are also removed no matter what, because of the forced bottom removals.
So these positions get -.
Guaranteed remaining cards
A card can only be removed from the top if it is among the first cards, because that is the maximum possible number of top removals.
A card can only be removed from the bottom if it is among the last cards.
So a card is definitely safe only if it is outside both danger zones:
Those positions get +.
If this interval is empty, then no card is guaranteed to remain. That is fine. Sometimes the deck is basically Schroedinger's cardboard.
Uncertain cards
Every remaining card is ?.
Why? Because it is not forced to be removed, but it is also within reach of some assignment of the 2 operations. For example, a card near the top can be removed if enough 2s are spent on top, but it can survive if those 2s are spent on bottom instead. Same idea near the bottom.
So the construction is simple:
?.-.-.+.This is per test case, which is comfortably fine because the total is at most .
#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, k;
string s;
cin >> n >> k >> s;
int top = 0, bottom = 0, either = 0;
for (char ch : s) {
if (ch == '0') top++;
else if (ch == '1') bottom++;
else either++;
}
string ans(n, '?');
for (int i = 0; i < top; i++) ans[i] = '-';
for (int i = n - bottom; i < n; i++) ans[i] = '-';
int safeL = top + either;
int safeR = n - bottom - either;
for (int i = safeL; i < safeR; i++) ans[i] = '+';
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, k;
string s;
cin >> n >> k >> s;
int top = 0, bottom = 0, either = 0;
for (char ch : s) {
if (ch == '0') top++;
else if (ch == '1') bottom++;
else either++;
}
string ans(n, '?');
for (int i = 0; i < top; i++) ans[i] = '-';
for (int i = n - bottom; i < n; i++) ans[i] = '-';
int safeL = top + either;
int safeR = n - bottom - either;
for (int i = safeL; i < safeR; i++) ans[i] = '+';
cout << ans << '\n';
}
}