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 ll long long
#define ld long double
#define vt vector
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
#pragma GCC optimize ("O3")
#pragma GCC optimize ("O2")
//#define endl '\n'
//#define int long long
using namespace std;
bool exists(vt<int>& master, int m)
{
auto it = lower_bound(all(master), m);
if (it == master.end()) return 0;
return (*it == m);
}
int minimumInstructions(int N, int M, int K, vt<int> C, vt<int> A, vt<vt<int>> B)
{
vt<vt<int>> master(K);
for(int i = 0; i < M; i++)
for(int j = 0; j < A[i]; j++) master[B[i][j]].pb(i);
vt<vt<pair<int, int>>> dp(N);
vt<int> dpMax(N);
for(int m : master[C[0]]) dp[0].pb({m, 1});
for(int i = 1; i < N; i++)
{
for(int m : master[C[i]])
{
int preM = (m + M - 1) % M, len = 1;
auto it = lower_bound(all(dp[i - 1]), make_pair(preM, 0));
if (it != dp[i - 1].end())
if ((*it).first == preM) len = min(M, (*it).second + 1);
dp[i].pb({m, len});
dpMax[i] = max(dpMax[i], len);
}
}
int ans = 0, pos = -1;
for(int i = M - 1, d = 1, flag = 0; i < N && d > 0; i++, d--)
{
//cout << i << " " << d << " " << pos << " " << flag << endl;
if (dpMax[i] == M) pos = i, flag = 1;
//for(auto m : dp[i]) cout << m.first << " " << m.second << endl;
//cout << endl;
if ((d == 1 || i == N - 1) && flag) ans++, d = M - (i - pos) + 1, flag = 0;
}
if (pos == N - 1) return ans;
return -1;
}
/**
main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
freopen("input.txt", "r", stdin);
int N, M, K;
cin >> N >> M >> K;
vt<int> C(N), A(M);
vt<vt<int>> B(M);
for(int i = 0; i < N; i++) cin >> C[i];
for(int i = 0; i < M; i++)
{
cin >> A[i];
B[i].resize(A[i]);
for(int j = 0; j < A[i]; j++) cin >> B[i][j];
}
//cout << M << endl;
int ans = minimumInstructions(N, M, K, C, A, B);
cout << ans << endl;
//int ans = minimumInstructions(6, 4, 4, {2, 1, 0, 3, 2, 1}, {3}, {{2}, {1}, {0}, {3}});
//cout << ans << endl;
}
/**/
Compilation message (stderr)
paint.cpp:79:1: warning: "/*" within comment [-Wcomment]
79 | /**/
|
# | 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... |