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 intervals longer than at first. If an interval has length at least and contains , it contains some sub-interval of length exactly that still contains .
So each 1 position only needs to avoid being the maximum in every length- window that contains it. Longer intervals then automatically behave.
What instantly makes the task impossible? If there is a length- window made entirely of 1s, the maximum inside that window sits at some 1 position, and that poor index is cooked.
If every length- window contains at least one 0, then give all 0 positions bigger numbers than all 1 positions. Now every constrained window has a bigger 0 to beat every 1.
Thus the whole problem is just: check whether the longest consecutive run of 1s has length at least . If yes, print NO; otherwise print any permutation where zeros get the largest values and ones get the smallest values.
The condition looks scarier than it is. The trick is to stop staring at all intervals, because most of them are just noisy extras.
For a position with , we need:
In every interval of length at least containing , is not the maximum.
Key simplification: it is enough to check intervals of length exactly .
Why? Take any interval with length at least that contains . Inside it, we can always choose a length- subarray that still contains . If is not the maximum inside that smaller subarray, then it definitely is not the maximum inside the larger interval either.
So the real requirement is:
For every index where , every length- window containing must contain some value larger than .
Now look at one length- window.
If this window contains only 1s, then no construction can work. Among those positions, one has the largest value. That position is a 1, and in this exact length- interval it is the maximum. That violates the rule immediately. No clever permutation magic saves it. Math says no; pack it up.
So a necessary condition is:
Now the nice part: this condition is also sufficient.
Suppose every length- window contains at least one 0. Then construct the permutation like this:
1 position gets one of the small values,0 position gets one of the large values.For example, if there are ones, give the ones values , and give the zeros values .
Now take any constrained position with , and take any length- window containing it. Since there are no consecutive ones, this window has at least one 0. That 0 has a larger value than every 1, including . Therefore is not the maximum.
And since length- windows imply all longer intervals, the whole condition is satisfied.
So the full algorithm is brutally simple:
1s.NO.YES and build a permutation where:
1 positions receive increasing small values,0 positions receive increasing large values.The values do not need fancy ordering inside each group. The only thing that matters is:
That single inequality does all the heavy lifting.
Complexity: Each test case is processed in time, and the total input size is bounded by , so this easily fits.
#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 cur = 0, best = 0, ones = 0;
for (char c : s) {
if (c == '1') {
cur++;
ones++;
} else {
cur = 0;
}
best = max(best, cur);
}
if (best >= k) {
cout << "NO\n";
continue;
}
vector<int> p(n);
int small = 1, large = ones + 1;
for (int i = 0; i < n; i++) {
if (s[i] == '1') p[i] = small++;
else p[i] = large++;
}
cout << "YES\n";
for (int i = 0; i < n; i++) {
if (i) cout << ' ';
cout << p[i];
}
cout << '\n';
}
return 0;
}#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 cur = 0, best = 0, ones = 0;
for (char c : s) {
if (c == '1') {
cur++;
ones++;
} else {
cur = 0;
}
best = max(best, cur);
}
if (best >= k) {
cout << "NO\n";
continue;
}
vector<int> p(n);
int small = 1, large = ones + 1;
for (int i = 0; i < n; i++) {
if (s[i] == '1') p[i] = small++;
else p[i] = large++;
}
cout << "YES\n";
for (int i = 0; i < n; i++) {
if (i) cout << ' ';
cout << p[i];
}
cout << '\n';
}
return 0;
}