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>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pi = pair<int,int>;
#define all(v) v.begin(),v.end()
#define sort(v) sort(all(v))
#define endl '\n'
#define forn(i,n) for(int i = 0; i < n; ++i)
#define forbe(i,b,e) for(int i = b; i < e; ++i)
#define forr(i,n) for(int i = n-1; i >= 0; --i)
#define sz(v) ((int)v.size())
#define pb push_back
#define f first
#define s second
struct aib{
vi f;
int n;
aib(int _n) {
n = _n;
f.assign(n,0);
}
void add(int x, int v) {
for(;x <n; x |= (x+1))
f[x] += v;
}
int q(int x) {
int r = 0;
for(;x >= 0; x = (x&(x+1))-1)
r += f[x];
return r;
}
int query(int l, int r) {
return q(r) - q(l-1);
}
};
string solve_puzzle(string s, vi c) {
int n = sz(s);
int k = sz(c);
string ans = s;
vi type(n,0);
vi pnt(k,0);
aib tr(n);
aib posseg(n+5);
aib minseg(n+5);
forn(i,n)
if (s[i] == '_')
tr.add(i,1);
int j = 0;
forn(i,n) {
if (j == k)
break;
if (tr.query(i,i+c[j]-1) > 0)
continue;
forn(l,c[j]) type[i+l] = 1;
pnt[j] = i;
i = i + c[j];
++j;
continue;
}
assert(j == k);
forn(j,k) {
minseg.add(pnt[j]+c[j],1);
if (j < k-1)
minseg.add(pnt[j+1],-1);
else
minseg.add(n,-1);
}
int lm;
int lp = n+1;
forr(j,k) {
lm = lp;
lp = pnt[j];
forbe(i,pnt[j], lm - c[j]) {
// cout << i << ' ' << i + c[j] << ' ' << tr.query(i,i+c[j]) << endl;
if (tr.query(i,i+c[j]-1) > 0)
continue;
// cout << j << ' ' << i << ' ' << n << endl;
lp = i;
posseg.add(i,1);
posseg.add(i+c[i],-1);
}
minseg.add(pnt[j],1);
minseg.add(lp,-1);
// cout << pnt[j] << ' ' << lp << endl;
}
forn(i,n) {
if (posseg.q(i) > 0) {
if (minseg.q(i) > 0)
ans[i] = '?';
else
ans[i] = 'X';
}
else
ans[i] = '_';
}
return ans;
}
#ifdef ONPC
void solve() {
int n, k;
cin >> k;
string s;
cin >> s;
vi c(k);
forn(i,k)
cin >> c[i];
string ans = solve_puzzle(s,c);
cout << ans << endl;
}
int main() {
freopen("in", "r", stdin);
// ios_base::sync_with_stdio(0);cin.tie(0);
solve();
}
#endif
# | 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... |