This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// ~~ Be name khoda ~~
#include "paint.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) x.begin(), x.end()
#define pb push_back
#define fi first
#define se second
const int maxn5 = 1e5 + 10;
const int maxn = 2e4 + 10;
const int maxm = 2e3 + 10;
int have[maxn][maxm], last[maxn5];
vector <int> out;
bool mark[maxn5];
int minimumInstructions(int N, int M, int K, vector<int> C, vector<int> A, vector<vector<int>> B){
int n = N, m = M;
if(n > maxn || m > maxm){
memset(last, -1, sizeof last);
for(int i = 0; i < m; i++) for(auto u : B[i])
last[u] = i;
bool re = true;
for(int i = 0; i < n; i++)
re &= (last[C[i]] != -1);
if(m == 1 || !re)
return (re ? n : -1);
int pos = last[C[0]], len = 1;
out.pb(-1);
for(int i = 1; i < n; i++){
if(last[C[i]] == (pos + 1) % m)
len++;
else
len = 1;
pos = last[C[i]];
if(len >= m){
while(out.size() > 1 && i - out[out.size() - 2] <= m)
out.pop_back();
if(i - out.back() > m)
return -1;
out.pb(i);
}
//cout << i << ' ' << C[i] << ' ' << pos << ' ' << len << ' ' << out.size() << endl;
}
if(out.back() != n - 1)
return -1;
return out.size() - 1;
}
for(int i = 0; i < m; i++){
memset(mark, false, sizeof mark);
for(auto u : B[i])
mark[u] = true;
for(int j = 0; j < n; j++)
have[j][i] = mark[C[j]];
}
if(m == 1){
bool re = true;
for(int i = 0; i < n; i++)
re &= have[i][0];
return (re ? n : -1);
}
out.pb(-1);
for(int i = 1; i < n; i++){
bool good = false;
for(int j = 0; j < m; j++) if(have[i][j]){
have[i][j] = have[i - 1][j == 0 ? m - 1 : j - 1] + 1;
good |= (have[i][j] >= m);
}
if(good){
while(out.size() > 1 && i - out[out.size() - 2] <= m)
out.pop_back();
if(i - out.back() > m)
return -1;
out.pb(i);
}
//cout << i << ' ' << good << ' ' << out.size() << endl;
}
if(out.back() != n - 1)
return -1;
return out.size() - 1;
}
// hen?
# | 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... |