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>
#ifdef MINA
#include "grader.cpp"
#endif
#include "paint.h"
using namespace std;
#define ll long long
#define SZ(x) (int) x.size()
const int N = 100'005, M = 50'005;
vector<int> gud[N + M + 5];
vector<int> col[N];
int ptr1[N + M + 5], ptr2[N + M + 5];
int minimumInstructions(int n, int m, int K, vector<int> a, vector<int> sz, vector<vector<int>> b) {
for (int i = 0; i < m; i++) {
for (auto j : b[i]) {
col[j].push_back(i);
}
}
for (int i = 0; i < n; i++) {
for (auto j : col[a[i]]) {
gud[i - j + M].push_back(i);
}
}
for (int i = -m; i <= n; i++) {
ptr1[i + M] = ptr2[i + M] = SZ(gud[i + M]) - 1;
}
int dp[n + 1]{};
dp[n] = 0;
for (int i = n - 1; i > n - m; i--) {
dp[i] = 1e9;
}
multiset<int> cur;
for (int i = n - m + 2; i <= n; i++) {
cur.insert(dp[i]);
}
for (int i = n - m; i >= 0; i--) {
cur.insert(dp[i + 1]);
if (i + m + 1 <= n) {
cur.erase(cur.find(dp[i + m + 1]));
}
dp[i] = 1e9;
bool ok = 0;
for (auto j : col[a[i]]) {
bool ok2 = 1;
int st = i + m - j;
while (ptr1[i - j + M] >= 0 && gud[i - j + M][ptr1[i - j + M]] > i) {
ptr1[i - j + M]--;
}
ok2 &= ptr1[i - j + M] >= 0 && gud[i - j + M][ptr1[i - j + M]] == i;
ok2 &= ptr1[i - j + M] + m - j - 1 < SZ(gud[i - j + M]) && gud[i - j + M][ptr1[i - j + M] + m - j - 1] == st - 1;
if (st <= i + m - 1) {
while (ptr2[st + M] >= 0 && gud[st + M][ptr2[st + M]] > i + m - 1) {
ptr2[st + M]--;
}
ok2 &= ptr2[st + M] >= 0 && gud[st + M][ptr2[st + M]] == i + m - 1;
ok2 &= ptr2[st + M] - (i + m - 1 - st) >= 0 && gud[st + M][ptr2[st + M] - (i + m - 1 - st)] == st;
// int s = lower_bound(gud[st + M].begin(), gud[st + M].end(), st) - gud[st + M].begin();
// int e = lower_bound(gud[st + M].begin(), gud[st + M].end(), i + m - 1) - gud[st + M].begin();
// ok2 &= e - s + 1 == i + m - st;
// ok2 &= e < SZ(gud[st + M]) && gud[st + M][e] == i + m - 1 && gud[st + M][s] == st;
}
ok |= ok2;
}
if (ok) {
dp[i] = *cur.begin() + 1;
}
}
if (dp[0] >= (int)1e9) return -1;
return dp[0];
}
# | 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... |