# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
548168 | Soumya1 | Painting Walls (APIO20_paint) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}