#include "paint.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
#define sz(v) (int)v.size()
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
const int MAXN = 1e5+7;
int n, m, k;
int color[MAXN], can[MAXN], L[MAXN];
//~ int rightmost[2][MAXN];
vector <int> isLiked[MAXN];
vector <pii> dp;
int g(int i) {
if (i >= n) return 0;
else if (i - L[i] >= m) return 1e9;
return 1+g(L[i]+m);
}
int reversed_search(int x) {
int l = 0, r = sz(dp)-1;
while (l < r) {
int mid = (l+r) >> 1;
if (dp[mid].first == x) return mid;
if (dp[mid].first < x) {
r = mid-1;
}
else {
l = mid+1;
}
}
if (dp[l].first == x) return l;
return -1;
}
int minimumInstructions(int N, int M, int K, vector<int> C, vector<int> A, vector<vector<int>> B) {
n = N; m = M; k = K;
for (int i = 0; i < n; i++) color[i] = C[i];
for (int i = 0; i < m; i++) {
for (auto to : B[i]) {
isLiked[to].pb(i);
}
}
for (int i = n-1; i >= 0; i--) {
vector <pii> curr_dp;
for (int j = sz(isLiked[color[i]])-1; j >= 0; j--) {
auto to = isLiked[color[i]][j];
if (i == n-1) {
curr_dp.pb(mp(to, i));
}
else {
auto it = reversed_search((to+1)%m);
if (it == -1) {
curr_dp.pb(mp(to, i));
}
else {
curr_dp.pb(mp(to, dp[it].second));
}
}
}
for (int j = 0; j < sz(isLiked[color[i]]); j++) {
if (curr_dp[j].second-i >= m-1) {
can[i] = 1;
}
}
dp = curr_dp;
}
int prev = -1e9;
for (int i = 0; i < n; i++) {
if (can[i]) prev = i;
L[i] = prev;
}
auto ans = g(0);
return (ans >= 1e8 ? -1 : ans);
}
/*
8 3 5
3 3 1 3 4 4 2 2
3 0 1 2
2 2 3
2 3 4
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Runtime error |
6 ms |
5228 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Runtime error |
6 ms |
5228 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Runtime error |
6 ms |
5228 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Runtime error |
6 ms |
5228 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2668 KB |
Output is correct |
2 |
Correct |
2 ms |
2668 KB |
Output is correct |
3 |
Runtime error |
6 ms |
5228 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |