This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
#ifdef mlocal
#include "./Competitive-Code-Library/snippets/prettyprint.h"
#endif
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
//#define endl '\n'
int main() {
#ifdef mlocal
freopen("test.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k; cin >> n >> k;
string s; cin >> s;
vi pref(n);
vector<deque<int>> prefpos(3*n);
for_(i, 0, n) {
if (s[i] == 'c') pref[i] = -k;
else pref[i] = 1;
if (i) pref[i] += pref[i-1];
prefpos[pref[i]+n].push_back(i);
}
function<void(int, int)> solve = [&] (int l, int r) {
if (l > r) return;
// cout << "solving " << l << " " << r << endl;
int prepref = (l > 0 ? pref[l-1] : 0);
// cout << "\tgot previous prefix: " << prepref << endl;
assert(pref[r] == prepref);
for_(split, l, r) {
if (pref[split] == prepref) {
solve(l, split);
solve(split+1, r);
return;
}
}
prefpos[prepref+n].pop_front();
for (int opt = k+1; opt >= 0; opt--) {
int va = (l+opt-1 >= 0 ? pref[l+opt-1] : 0) - prepref;
int sufopt = k+1-opt;
int vb = pref[r] - (r-sufopt >= 0 ? pref[r-sufopt] : 0);
if (va+vb == 0) {
// cout << "works with prefix " << opt << endl;
vi ans;
for_(i, l, l+opt) ans.push_back(i);
for_(i, r-sufopt+1, r+1) ans.push_back(i);
for (auto i: ans) cout << i+1 << " ";
cout << endl;
// cout << "should have gone to " << l+opt << " " << r-sufopt << " from " << l << " " << r << endl;
solve(l+opt, r-sufopt);
return;
}
}
// cout << "uhhh found nothing " << l << " " << r << endl;
// assert(false);
};
solve(0, n-1);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |