# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
548168 | Soumya1 | 벽 칠하기 (APIO20_paint) | C++17 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "paint.h"
#include <bits/stdc++.h>
#include <vector>
#include <debug_local.h>
using namespace std;
int minimumInstructions(int n, int m, int k, vector<int> c, vector<int> a, vector<vector<int>> b) {
vector<int> l[k];
for (int i = 0; i < m; i++) {
for (int j : b[i]) {
l[j].push_back(i);
}
}
vector<int> last;
vector<int> f(n);
for (int j : l[c[n - 1]]) last.push_back(1);
for (int i = n - 2; i >= 0; i--) {
int ptr = 0;
vector<int> new_last;
for (int j = 0; j < l[c[i]].size(); j++) {
while (ptr < l[c[i + 1]].size() && l[c[i + 1]][ptr] < (1 + l[c[i]][j]) % m) ptr++;
if (l[c[i]][j] == m - 1 && l[c[i + 1]].size() && l[c[i + 1]][0] == 0) new_last.push_back(last[0] + 1);
else if (ptr < l[c[i + 1]].size() && l[c[i + 1]][ptr] == (1 + l[c[i]][j]) % m) new_last.push_back(last[ptr] + 1);
else new_last.push_back(1);
}
last = new_last;
f[i] = (*max_element(last.begin(), last.end()) >= m);
}
vector<int> s;
for (int i = n; i > 0; i--) s.push_back(i);
queue<int> q;
q.push(0);
vector<int> dp(n + 1, 1e9);
dp[0] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
if (!f[u]) continue;
dp[u]++;
while (!s.empty() && s.back() <= u + m) {
q.push(s.back());
dp[s.back()] = dp[u];
s.pop_back();
}
}
return (dp[n] >= 1e9 ? -1 : dp[n]);
return 0;
}