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 <bits/stdc++.h>
#include "paint.h"
using namespace std;
int sum(int a, int b) {
return a * b;
}
const int INF = (int) 1e9;
int minimumInstructions(int n, int m, int k, vector<int> c, vector<int> a, vector<vector<int>> b) {
vector<int> can(n, 0);
vector<set<int>> match(m);
vector<vector<int>> guys(k);
for (int i = 0; i < m; i++) {
for (auto &j : b[i]) {
match[i].insert(j);
guys[j].push_back(i);
}
}
deque<int> dq;
for (int i = 0; i + m - 1 < n; i++) {
/// can[i] = 1?
/// de la i la i + m - 1 imi pasa de cel cu dimensiunea minima
while (!dq.empty() && dq.front() < i) {
dq.pop_front();
}
if (i == 0) {
for (int j = 0; j < m; j++) {
while (!dq.empty() && (int) guys[c[j]].size() <= (int) guys[c[dq.back()]].size()) {
dq.pop_back();
}
dq.push_back(j);
}
} else {
int j = i + m - 1;
while (!dq.empty() && (int) guys[c[j]].size() <= (int) guys[c[dq.back()]].size()) {
dq.pop_back();
}
dq.push_back(j);
}
int raz = dq.front() - i;
for (auto &val : guys[c[i + raz]]) {
int x = (val - raz + m) % m;
assert(x >= 0);
bool good = 1;
for (int l = 0; l < m; l++) {
if (!match[(x + l) % m].count(c[i + l])) {
good = 0;
break;
}
}
if (good) {
can[i] = 1;
break;
}
}
}
{ /// shift can by 1
reverse(can.begin(), can.end());
can.push_back(0);
reverse(can.begin(), can.end());
}
vector<int> dp(n + 1, INF);
dp[0] = 0;
dq.clear();
dq.push_back(0);
for (int i = m; i <= n; i++) {
/// finish at position i => I've started at position i-k+1
int start = i - m + 1;
if (!can[start]) {
continue;
}
while (!dq.empty() && dq.front() < start - 1) {
dq.pop_front();
}
while (!dq.empty() && dp[i] <= dp[dq.back()]) {
dq.pop_back();
}
if (!dq.empty()) {
dp[i] = dp[dq.front()] + 1;
}
dq.push_back(i);
}
if (dp[n] == INF) {
dp[n] = -1;
}
return dp[n];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |