이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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], rightmost[20003][2003], L[MAXN], rightmost1[MAXN];
vector <int> isLiked[MAXN];
int g(int i) {
if (i >= n) return 0;
else if (i - L[i] >= m) return 1e9;
return 1+g(L[i]+m);
}
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);
}
}
int fk = 0;
for (int i = 0; i < k; i++) {
fk = max(fk, sz(isLiked[i]));
}
if (fk < 2) {
vector <int> which(n);
for (int i = 0; i < n; i++) {
if (sz(isLiked[color[i]]) == 0) return -1;
which[i] = isLiked[color[i]].back();
}
for (int i = n-1; i >= 0; i--) {
if (i == n-1 || which[i+1] != (which[i]+1)%m) {
rightmost1[i] = i;
}
else {
rightmost1[i] = rightmost1[i+1];
}
}
for (int i = 0; i < n; i++) {
can[i] = (rightmost1[i]-i >= m-1 ? 1 : 0);
}
}
else {
for (int i = n-1; i >= 0; i--) {
for (int j = m-1; j >= 0; j--) {
if (!binary_search(all(isLiked[color[i]]), j)) {
rightmost[i][j] = -1e9;
}
else {
if (i == n-1 || !binary_search(all(isLiked[color[i+1]]), (j+1)%m)) {
rightmost[i][j] = i;
}
else {
rightmost[i][j] = rightmost[i+1][(j+1)%m];
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (rightmost[i][j]-i >= m-1) {
can[i] = 1;
}
}
}
}
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
*/
# | 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... |