//from duckindog wth depression
#include<bits/stdc++.h>
using namespace std;
//#define LOCAL
#ifndef LOCAL
#include "paint.h"
#endif // LOCAL
const int N = 1e5 + 10;
int f[N], g[N];
bool pass[N];
vector<int> p[N];
int minimumInstructions(int n, int m, int k, vector<int> C, vector<int> A, vector<vector<int>> B) {
for (int i = 0; i < m; ++i) {
for (auto &x : B[i]) p[x].push_back(i);
}
for (int i = 0; i < n; ++i) {
for (auto &x : p[C[i]]) {
f[x] = g[(x - 1 + m) % m] + 1;
if (f[x] >= m) pass[i + 1] = 1;
}
if (i) for (auto &x : p[C[i - 1]]) g[x] = 0;
for (auto &x : p[C[i]]) {
g[x] = f[x];
f[x] = 0;
}
}
memset(f, 63, sizeof f);
deque<int> q;
q.push_back(f[0] = 0);
for (int i = m; i <= n; ++i) {
if (!pass[i]) continue;
while (q.size() && q.back() < i - m) q.pop_back();
f[i] = f[q.back()] + 1;
while (q.size() && f[q.front()] >= f[i]) q.pop_front();
q.push_front(i);
}
return f[n] > 1e9 ? -1 : f[n];
}
#ifdef LOCAL
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("duck.inp", "r")) {
freopen("duck.inp", "r", stdin);
freopen("duck.out", "w", stdout);
}
int n, m, k; cin >> n >> m >> k;
vector<int> C, A;
vector<vector<int>> B;
for (int i = 1; i <= n; ++i) {
int x; cin >> x;
C.push_back(x);
}
for (int i = 1; i <= m; ++i) {
int a; cin >> a; A.push_back(a);
vector<int> vt;
for (int j = 1; j <= a; ++j) {
int x; cin >> x;
vt.push_back(x);
}
sort(vt.begin(), vt.end());
B.push_back(vt);
}
cout << minimumInstructions(n, m, k, C, A, B);
}
#endif // LOCAL
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3164 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
2 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
1 ms |
3164 KB |
Output is correct |
6 |
Correct |
1 ms |
3416 KB |
Output is correct |
7 |
Correct |
1 ms |
3164 KB |
Output is correct |
8 |
Correct |
1 ms |
3048 KB |
Output is correct |
9 |
Runtime error |
4 ms |
5980 KB |
Execution killed with signal 11 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3164 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
2 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
1 ms |
3164 KB |
Output is correct |
6 |
Correct |
1 ms |
3416 KB |
Output is correct |
7 |
Correct |
1 ms |
3164 KB |
Output is correct |
8 |
Correct |
1 ms |
3048 KB |
Output is correct |
9 |
Runtime error |
4 ms |
5980 KB |
Execution killed with signal 11 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3164 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
2 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
1 ms |
3164 KB |
Output is correct |
6 |
Correct |
1 ms |
3416 KB |
Output is correct |
7 |
Correct |
1 ms |
3164 KB |
Output is correct |
8 |
Correct |
1 ms |
3048 KB |
Output is correct |
9 |
Runtime error |
4 ms |
5980 KB |
Execution killed with signal 11 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3164 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
2 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
1 ms |
3164 KB |
Output is correct |
6 |
Correct |
1 ms |
3416 KB |
Output is correct |
7 |
Correct |
1 ms |
3164 KB |
Output is correct |
8 |
Correct |
1 ms |
3048 KB |
Output is correct |
9 |
Runtime error |
4 ms |
5980 KB |
Execution killed with signal 11 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
3164 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
2 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
1 ms |
3164 KB |
Output is correct |
6 |
Correct |
1 ms |
3416 KB |
Output is correct |
7 |
Correct |
1 ms |
3164 KB |
Output is correct |
8 |
Correct |
1 ms |
3048 KB |
Output is correct |
9 |
Runtime error |
4 ms |
5980 KB |
Execution killed with signal 11 |
10 |
Halted |
0 ms |
0 KB |
- |