Submission #551633

#TimeUsernameProblemLanguageResultExecution timeMemory
551633Theo830Paint By Numbers (IOI16_paint)C++17
59 / 100
37 ms79436 KiB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
///I hope I will get uprating and don't make mistakes
///I will never stop programming
///sqrt(-1) Love C++
///Please don't hack me
///@TheofanisOrfanou Theo830
///Think different approaches (bs,dp,greedy,graphs,shortest paths,mst)
///Stay Calm
///Look for special cases
///Beware of overflow and array bounds
///Think the problem backwards
///Training
#include "paint.h"
ll ans[200005] = {0};
vector<ii>ex;
ll n,k;
string S;
vector<int>C;
int dp[200005][101];
ll Next[200005];
int solve(ll idx,ll p){
    if(idx == n){
        return (p == k);
    }
    if(dp[idx][p] != -1){
        return dp[idx][p];
    }
    bool ok = 0;
    if(S[idx] == '_'){
        ok = solve(idx + 1,p);
        ans[idx] |= ok;
        return dp[idx][p] = ok;
    }
    if(p != k){
        if(idx + C[p] > n){
            ok = 0;
        }
        else if(Next[idx] >= n && idx + C[p] == n){
            ok = solve(idx + C[p],p + 1);
            if(ok){
                ex.pb(ii(idx,idx + C[p] - 1));
            }
        }
        else{
            if(Next[idx] >= idx + C[p]){
                ok = solve(idx + C[p] + 1,p + 1);
                ok &= S[idx + C[p]] != 'X';
                if(ok){
                    ex.pb(ii(idx,idx + C[p] - 1));
                    ans[idx + C[p]] = 1;
                }
            }
            else{
                ok = 0;
            }
        }
    }
    else{
        if(S[idx] == 'X'){
            return dp[idx][p] = 0;
        }
    }
    if(S[idx] == '.'){
        bool v = solve(idx + 1,p);
        ans[idx] |= v;
        ok |= v;
    }
    return dp[idx][p] = ok;
}
std::string solve_puzzle(std::string s, std::vector<int> c){
    n = s.size();
    k = c.size();
    S = s;
    C = c;
    Next[n-1] = INF;
    for(ll i = n-2;i >= 0;i--){
        Next[i] = Next[i+1];
        if(s[i+1] == '_'){
            Next[i] = i+1;
        }
    }
    memset(dp,-1,sizeof dp);
    solve(0,0);
    string res = "";
    sort(all(ex));
    ll r = 0;
    for(auto x:ex){
        if(x.F > r){
            r = x.F;
        }
        while(r <= x.S){
            ans[r] |= 2;
            r++;
        }
    }
    f(i,0,n){
        assert(ans[i]);
        if(ans[i] == 3){
            res += "?";
        }
        else if(ans[i] == 2){
            res += "X";
        }
        else{
            res += "_";
        }
    }
    return res;
}
/*
const int S_MAX_LEN = 200 * 1000;
char buf[S_MAX_LEN + 1];

int main() {
    assert(1 == scanf("%s", buf));
    std::string s = buf;
    int c_len;
    assert(1 == scanf("%d", &c_len));
    std::vector<int> c(c_len);
    for (int i = 0; i < c_len; i++) {
        assert(1 == scanf("%d", &c[i]));
    }
    std::string ans = solve_puzzle(s, c);


    printf("%s\n", ans.data());
    return 0;
}

*/

#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...