제출 #305643

#제출 시각아이디문제언어결과실행 시간메모리
305643tatyam벽 칠하기 (APIO20_paint)C++17
100 / 100
680 ms15216 KiB
#include <bits/stdc++.h>
using namespace std;
void chmin(int& a, int b){ if(a > b) a = b; }
void chmax(int& a, int b){ if(a < b) a = b; }

int minimumInstructions(int N, int M, int K, vector<int> C, vector<int> A, vector<vector<int>> B){
    vector range(M, pair{-1, -1});
    vector next(N, 0);
    vector trB(K, vector<int>{});
    for(int i = 0; i < M; i++) for(int c : B[i]) trB[c].push_back(i);
    for(int i = N; i--; ){
        const int c = C[i];
        for(int b : trB[c]){
            int x = (b - i) % M;
            if(x < 0) x += M;
            auto& [l, r] = range[x];
            if(l != i + 1) r = i + 1;
            l = i;
            if(r - l >= M) chmax(next[i], r);
        }
    }
    for(int i = 0; i < N - 1; i++) chmax(next[i + 1], next[i]);
    for(int i = 0; i < N; i++) chmin(next[i], i + M);
    int ans = 0, at = 0;
    while(at < N){
        if(next[at] <= at) return -1;
        at = next[at];
        ans++;
    }
    return ans;
}
#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...