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>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 2e5 + 10;
bool good[N];
int n, m, k;
int f[N], g[N], bit[N], dp[N];
vector<int> color[N];
void add(int pos, int val) {
while (pos <= n) {
bit[pos] = min(bit[pos], val);
pos += pos & -pos;
}
}
int get(int pos) {
int ret = 1e9;
while (pos) {
ret = min(ret, bit[pos]);
pos -= pos & -pos;
}
return ret;
}
int minimumInstructions(int N, int M, int K, std::vector<int> c, std::vector<int> a, std::vector<std::vector<int>> b) {
n = N; m = M; k = K;
for(int i = 0; i < m; i++) {
for(int &j : b[i]) {
color[j].push_back(i);
}
}
for(int i = n - 1; i >= 0; i--) {
for(int &j : color[c[i]]) {
f[j] = g[(j + 1) % m] + 1;
if (f[j] >= m) good[i] = 1;
}
if (i + 1 < n) for(int &j : color[c[i + 1]]) g[j] = 0;
for(int &j : color[c[i]]) {
g[j] = f[j];
f[j] = 0;
}
}
for(int i = 0; i < n; i++) dp[i] = bit[i] = 1e9;
if (!good[n - m]) return -1;
dp[n - m] = 1;
if (n - m) add(n - m, 1);
for(int i = n - m - 1; i >= 0; i--) if (good[i]) {
dp[i] = get(i + m) + 1;
if (i) add(i, dp[i]);
}
if (dp[0] > 1e8) return -1;
return dp[0];
}
#ifdef ngu
int main() {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
int n, m, k;
cin >> n >> m >> k;
vector<int> c(n), a(m);
vector<vector<int> > b;
for(int i = 0; i < n; i++) cin >> c[i];
// for(int i = 0; i < n; i++) cout << c[i] << " "; cout << endl;
for(int i = 0; i < m; i++) {
cin >> a[i];
vector<int> tmp(a[i]);
for(int j = 0; j < a[i]; j++) cin >> tmp[j];
b.push_back(tmp);
}
cout << minimumInstructions(n, m, k, c, a, b);
}
#endif // ngu
# | 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... |