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;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<bool> vb;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,k,n) for(ll i=(ll)(k);i<(ll)(n);i++)
#define all(a) a.begin(),a.end()
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pb emplace_back
template<class T> void out(T a){cout<<a<<endl;}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<endl;}
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
const ll inf=1001001001001001001;
bool check(string s,vector<int> c){
string t;
rep(i,c.size()){
if(i)t+='_';
rep(j,c[i])t+='X';
}
vvi dp(s.size()+1,vi(t.size()+1));
dp[0][0]=1;
rep(i,s.size())rep(j,t.size()+1)if(dp[i][j]){
if(j<t.size()&&(s[i]=='.'||s[i]==t[j]))dp[i+1][j+1]=1;
if((j==0||j==t.size())&&s[i]!='X')dp[i+1][j]=1;
if(j>0&&s[i]!='X'&&t[j-1]=='_')dp[i+1][j]=1;
}
// out(s);
// out(dp[s.size()][t.size()]);
return dp[s.size()][t.size()];
}
std::string solve_puzzle1(std::string s, std::vector<int> c) {
string res=s;
rep(i,s.size())if(s[i]=='.'){
s[i]='X';
if(!check(s,c))res[i]='_';
else{
s[i]='_';
if(!check(s,c))res[i]='X';
else res[i]='?';
}
s[i]='.';
}
return res;
}
string solve_puzzle(string s,vector<int> c){
ll n=s.size(),K=c.size();
vvi dpl(n+1,vi(K+1)),dpr(n+1,vi(K+1));
dpl[0][0]=1;dpr[n][K]=1;
vi rui_(n+1),ruix(n+1);
rep(i,n){
if(s[i]=='_')rui_[i+1]++;
rui_[i+1]+=rui_[i];
if(s[i]=='X')ruix[i+1]++;
ruix[i+1]+=ruix[i];
}
auto rng=[&](ll l,ll r){
return rui_[r]-rui_[l];
};
rep(i,n)rep(j,K+1)if(dpl[i][j]){
if(s[i]!='X')dpl[i+1][j]=1;
if(i==0&&rng(0,c[0])==0)dpl[c[0]][1]=1;
if(j<K&&i+1+c[j]<=n&&s[i]!='X'&&rng(i+1,i+1+c[j])==0)dpl[i+1+c[j]][j+1]=1;
}
for(int i=n;i>0;i--)for(int j=K;j>=0;j--)if(dpr[i][j]){
if(s[i-1]!='X')dpr[i-1][j]=1;
if(i==n&&rng(n-c[K-1],n)==0)dpr[n-c[K-1]][K-1]=1;
if(j>0&&i-1-c[j-1]>=0&&s[i-1]!='X'&&rng(i-1-c[j-1],i-1)==0)dpr[i-1-c[j-1]][j-1]=1;
}
rep(i,n)if(s[i]=='.'){
bool flag=false;
rep(k,K+1)if(dpl[i][k]&&dpr[i+1][k])flag=true;
if(!flag)s[i]='X';
}
vi imos(n+1);
rep(k,K)rep(l,n-c[k]+1)if(rng(l,l+c[k])==0&&((l==0&&k==0)||(l!=0&&dpl[l-1][k]))&&((l+c[k]==n&&k==K-1)||(l+c[k]!=n&&dpr[l+c[k]+1][k+1]))&&(l==0||s[l-1]!='X')&&(l+c[k]==n||s[l+c[k]]!='X')){
imos[l]++;imos[l+c[k]]--;
}
rep(i,n)imos[i+1]+=imos[i];
rep(i,n)if(s[i]=='.'){
if(!imos[i])s[i]='_';
else s[i]='?';
}
return s;
}
/*
int main(){
string s="..___..X.___";
vector<int> c={2,1};
out(solve_puzzle(s,c));
out(solve_puzzle1(s,c));
rep(tt,100000){
vp v;
ll n=rand()%5+2;
rep(i,n){
P p(rand()%2,rand()%5+1);
if(v.size()&&v.back().fi==p.fi)v.back().se++;
else v.pb(p);
}
vector<int> c;
for(auto x:v)if(x.fi==0)c.pb(x.se);
if(c.size()==0)continue;
string s;
for(auto x:v){
char c='X';
if(x.fi==1)c='_';
rep(i,x.se)s+=c;
}
rep(i,s.size())if(rand()%3==0)s[i]='.';
string a=solve_puzzle(s,c);
string b=solve_puzzle1(s,c);
if(a!=b){
out(tt);
out(s);
outv(c);
out(a);out(b);
return 0;
}
}
}
*/
Compilation message (stderr)
paint.cpp: In function 'bool check(std::string, std::vector<int>)':
paint.cpp:33:13: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | if(j<t.size()&&(s[i]=='.'||s[i]==t[j]))dp[i+1][j+1]=1;
| ~^~~~~~~~~
paint.cpp:34:20: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | if((j==0||j==t.size())&&s[i]!='X')dp[i+1][j]=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... |