제출 #591028

#제출 시각아이디문제언어결과실행 시간메모리
591028jasminPaint By Numbers (IOI16_paint)C++14
80 / 100
40 ms25548 KiB
#include<bits/stdc++.h>
#include<paint.h>
using namespace std;

void make_dp_front(string& s, vector<int>& c, vector<vector<vector<int> > >& dp){
    int n=s.size();
    int k=c.size();

    vector<int> lastwhite(n);
    int last=-10;
    for(int i=0; i<n; i++){
        if(s[i]=='_'){
            last=i;
        }
        lastwhite[i]=last;
    }

    dp[0][0][0]=true;

    for(int i=1; i<=n; i++){
        for(int j=0; j<=k; j++){

            if(s[i-1]=='_'){//white
                dp[i][j][0]=dp[i-1][j][0] || dp[i-1][j][1];
                dp[i][j][1]=false;
            }
            else if(s[i-1]=='X'){ //black
                dp[i][j][0]=false;
                if(0<j && 0<=i-c[j-1] && lastwhite[i-1]+1<=i-c[j-1]){
                    dp[i][j][1]=dp[i-c[j-1]][j-1][0];
                }
            }
            else{
                dp[i][j][0]=dp[i-1][j][0] || dp[i-1][j][1];
                if(0<j && 0<=i-c[j-1] && lastwhite[i-1]+1<=i-c[j-1]){
                    dp[i][j][1]=dp[i-c[j-1]][j-1][0];
                }
            }
            
        }
    }
}
void make_dp_back(string& s, vector<int>& c, vector<vector<vector<int> > >& dp){
    int n=s.size();
    int k=c.size();

    vector<int> lastwhite(n);
    int last=n+10;
    for(int i=n-1; i>=0; i--){
        if(s[i]=='_'){
            last=i;
        }
        lastwhite[i]=last;
    }
    dp[n][k][0]=true;

    for(int i=n-1; i>=0; i--){
        for(int j=k; j>=0; j--){

            if(s[i]=='_'){//white
                dp[i][j][0]=dp[i+1][j][0] || dp[i+1][j][1];
                dp[i][j][1]=false;
            }
            else if(s[i]=='X'){ //black
                dp[i][j][0]=false;
                if(j<k && i+c[j]<=n && i+c[j]<=lastwhite[i]){
                    dp[i][j][1]=dp[i+c[j]][j+1][0];
                }
            }
            else{
                dp[i][j][0]=(dp[i+1][j][0] || dp[i+1][j][1]);
                if(j<k && i+c[j]<=n && i+c[j]<=lastwhite[i]){
                    dp[i][j][1]=dp[i+c[j]][j+1][0];
                }
            }
            
        }
    }
}


string solve_puzzle(string s, vector<int> c){
    int n=s.size(); 
    int k=c.size();

    vector<vector<vector<int> > > dp_front(n+1, vector<vector<int> > (k+1, vector<int> (2, false)));
    make_dp_front(s, c, dp_front);
    vector<vector<vector<int> > > dp_back(n+1, vector<vector<int> > (k+1, vector<int> (2, false)));
    make_dp_back(s, c, dp_back);

    /*for(int i=0; i<n; i++){
        cout << i << ": \n";
        for(int j=0; j<k; j++){
            cout << "( " << dp_front[i+1][j+1][0] << ", " << dp_front[i+1][j+1][1] << ") ";
        }
        cout << "\n";
    }
    for(int i=0; i<n; i++){
        cout << i << ": \n";
        for(int j=0; j<k; j++){
            cout << "( " << dp_back[i][j][0] << ", " << dp_back[i][j][1] << ") ";
        }
        cout << "\n" << flush;
    }*/

    vector<bool> black(n);
    vector<bool> white(n);
    for(int j=0; j<k; j++){
        int r=1; //exklusiv
        int last=0;
        for(int l=0; l<n; l++, r=min(r, l+1)){
            if(s[l]=='_'){
                continue;
            }
            while(l<n && r<=n && r-l<c[j]){
                if(s[r]=='_'){
                    l=r+1;
                    r=r+1;
                }
                r++;
            }

            if(l==n || r>n) break;

            //cout << j << ": " << l << " " << r << "\n" << flush;
            if(r-l==c[j] && dp_front[l][j][0] && dp_back[r][j+1][0]){
                //cout << "Yes " << last << "\n" << flush;
                last=max(last, l);
                for(int x=last; x<r; x++){
                    black[x]=true;
                    //cout << x << "true\n" << flush;
                }
                last=r;
            }
        }

    }
    for(int j=0; j<=k; j++){
        for(int i=0; i<n; i++){
            if((dp_front[i][j][0] || dp_front[i][j][1])
                && (dp_back[i+1][j][0] || dp_back[i+1][j][1])){

                    //cout << "ok white: " << i << "\n" << flush;
                    white[i]=true;
                }
        }
    }

    string ans=s;
    for(int i=0; i<n; i++){
        assert(black[i] || white[i]);
        if(s[i]=='_' || !black[i]){
            ans[i]='_';
        }
        else if(s[i]=='X' || !white[i]){
            ans[i]='X';
        }
        else{
            ans[i]='?';
        }
    }
    return ans;
}


/*signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    vector<int> a(k);
    for(int i=0; i<k; i++){
        cin >> a[i];
    }

    string ans=solve_puzzle(s, a);
    cout << ans << "\n";
}*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...