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 "paint.h"
#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0;i<n;++i)
#define pb push_back
#define all(x) x.begin(), x.end()
#define pi pair<int,int>
#define f first
#define s second
const int inf=1e9+9;
const int N=5555;
const int K=105;
int dp[N][K];
int dp2[N][K];
int can[N];
int cant[N];
int yes(int l, int r) {
for(int i=l; i<=r; ++i) cant[i]=0;
}
int no(int l, int r) {
for(int i=l;i<=r;++i) can[i]=0;
}
pi inter(pi a, pi b) {
return {max(a.f,b.f),min(a.s,b.s)};
}
string solve_puzzle(string s, vector<int> c) {
s+='_';
int n=s.size();
int k=c.size();
if (n>101) exit(0);
forn(i,n) can[i]=cant[i]=1;
vector<vector<int>> pos(k+1);
pos[0].pb(0);
dp[0][0]=1;
for(int i=1; i<=n; ++i) {
forn(j,k) {
int x=c[j]+1;
if (i-x<0) continue;
if (!pos[j].size()) continue;
if (pos[j][0]>i-x) continue;
int last;
for(auto&v:pos[j]) if (v<=i-x) last=v;
int y=1, z=1;
for (int t=i-x; t<i-1; ++t) y&=s[t]!='_';
y&=s[i-1]!='X';
for (int t=last; t<i-x; ++t) z&=s[t]!='X';
dp[i][j+1]=y&z;
if (dp[i][j+1]) pos[j+1].pb(i);
}
}
vector<vector<int>> pos2(k+2);
pos2[k+1].pb(n+1);
dp2[n+1][k+1]=1;
for (int i=n; i>0; --i) {
for(int j=k; j>0; --j) {
int x=c[j-1]+1;
if (i+x>n+1) continue;
if (!pos2[j+1].size()) continue;
if (pos2[j+1][0]<i+x) continue;
int last;
for(auto&v:pos2[j+1]) if (v>=i+x) last=v;
int y=1, z=1;
for (int t=i-1; t<i+x-2; ++t) y&=s[t]!='_';
y&=s[i+x-2]!='X';
for (int t=i+x-2; t<last-1; ++t) z&=s[t]!='X';
dp2[i][j]=y&z;
if (dp2[i][j]) pos2[j].pb(i);
}
}
for (int i=0; i<=n+1; ++i) dp[i][0]=dp2[i][k+1]=1;
vector<pi> seg(k+1,{-inf,inf});
for (int i=1; i<=n; ++i) {
for(int j=1; j<=k; ++j) {
int x=c[j-1]+1;
if (dp[i][j]) {
if (dp2[i+1][j+1]) {
yes(i-x,i-2);
seg[j]=inter(seg[j],{i-x,i-2});
}
}
}
}
string ans(n,'?');
forn(i,n) if (cant[i]) ans[i]='_';
for(int j=1; j<=k; ++j) {
auto x=seg[j];
if (x.f<=x.s) for(int i=x.f; i<=x.s; ++i) ans[i]='X';
}
forn(i,n) {
if (s[i]=='X') {
assert(ans[i]!='_');
ans[i]='X';
}
}
ans.pop_back();
return ans;
}
Compilation message (stderr)
paint.cpp: In function 'int yes(int, int)':
paint.cpp:22:1: warning: no return statement in function returning non-void [-Wreturn-type]
22 | }
| ^
paint.cpp: In function 'int no(int, int)':
paint.cpp:25:1: warning: no return statement in function returning non-void [-Wreturn-type]
25 | }
| ^
paint.cpp: In function 'std::string solve_puzzle(std::string, std::vector<int>)':
paint.cpp:69:17: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
69 | int last;
| ^~~~
paint.cpp:48:17: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
48 | int last;
| ^~~~
# | 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... |