제출 #572015

#제출 시각아이디문제언어결과실행 시간메모리
572015SlavicG벽 칠하기 (APIO20_paint)C++17
100 / 100
1257 ms411020 KiB
#include "bits/stdc++.h"
using namespace std;
 
#define ll long long
 
#define       forn(i,n)              for(int i=0;i<n;i++)
#define          all(v)              v.begin(), v.end()
#define         rall(v)              v.rbegin(),v.rend()
 
#define            pb                push_back
#define          sz(a)               (int)a.size()

int minimumInstructions(int n, int m, int k, vector<int> c, vector<int> a, vector<vector<int>> b) {
    vector<int> p[k + 1];

    unordered_set<int> s[m + 1];
    for(int i = 0; i < m; ++i) {
        for(auto x: b[i]) {
            p[x].pb(i);
            s[i].insert(x);
        }
    }  
    for(int i = 0; i <= k; ++i) {
        sort(all(p[i]));
        p[i].erase(unique(all(p[i])), p[i].end());
    }

    vector<vector<int>> mx(n);

    for(auto x: p[c[n - 1]]) mx[n - 1].pb(1);

    vector<int> bruh(n, 1);

    vector<int> pos(m, -1);
    for(int i = 0; i < sz(p[c[n - 1]]); ++i) {
        pos[p[c[n - 1]][i]] = i;
    }
    for(int i = n - 2; i >= 0; --i) {
        int pp = 0;
        for(auto x: p[c[i]]) {
            int newval = x + 1;
            if(newval >= m) newval -= m;
            if(s[newval].find(c[i + 1]) == s[newval].end()) {
                mx[i].pb(1);
                continue;
            }
            int idx = pos[newval];
            mx[i].pb(mx[i + 1][idx] + 1);
            bruh[i] = max(bruh[i], mx[i].back());
        }
        for(int j = 0; j < sz(p[c[i]]); ++j) {
            pos[p[c[i]][j]] = j;
        }
    }

    vector<bool> can(n, false);
    for(int i = 0; i < n; ++i) {
        for(auto x = 0; x < sz(mx[i]); ++x) {
            if(bruh[i] >= m) can[i] = true;
        }
    }


    set<int> good;
    forn(i, n) if(can[i]) good.insert(i); 
    if(good.empty()) return -1;
    if(!can[0]) return -1;
    int lst = -100000000, ans = 0;
    for(int i = 0; i < n; ++i) {
        if(lst + m - 1 >= i) continue;
        ++ans;
        if(can[i]) {
            lst = i;
        } else {
            auto it = good.lower_bound(i);
            if(it == good.begin()) return -1;
            --it;
            if(*it + m - 1 < i) return -1;
            lst = *it;
        }
    }
    return ans;
}

/*
void solve() {  
    int n, m, k; cin >> n >> m >> k;
    vector<int> c(n); forn(i, n) cin >> c[i];
    vector<int> a(m); forn(i, m) cin >> a[i];
    vector<vector<int>> b(m);
    for(int i = 0; i < m; ++i) {
        b[i].resize(a[i]);
        for(int j = 0; j < a[i]; ++j) {
            cin >> b[i][j];
        }
    }
    cout << minimumInstructions(n, m, k, c, a, b) << "\n";
} 
     
int32_t main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t = 1;
    //cin >> t;
    while(t--) {
        solve();
    }
}   

/*
8 3 5
3 3 1 3 4 4 2 2
3 2 2
0 1 2
2 3
3 4
*/

컴파일 시 표준 에러 (stderr) 메시지

paint.cpp:109:1: warning: "/*" within comment [-Wcomment]
  109 | /*
      |  
paint.cpp: In function 'int minimumInstructions(int, int, int, std::vector<int>, std::vector<int>, std::vector<std::vector<int> >)':
paint.cpp:30:14: warning: unused variable 'x' [-Wunused-variable]
   30 |     for(auto x: p[c[n - 1]]) mx[n - 1].pb(1);
      |              ^
paint.cpp:39:13: warning: unused variable 'pp' [-Wunused-variable]
   39 |         int pp = 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...