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>
#include "paint.h"
using namespace std;
const int INF = INT_MAX / 2;
const int M_MAX = 2000;
mt19937 rng(time(0));
int rngSeg (int l, int len)
{
return l + rng() % len;
}
int minimumInstructions(int N, int M, int K, vector <int> C, vector <int> A, vector <vector <int>> B)
{
vector <vector <int>> likers (K);
for(int i = 0; i < M; i++)
for(int j = 0; j < A[i]; j++)
likers[B[i][j]].push_back(i);
function <bool (int, int)> likes = [&] (int worker, int color)
{
int l = 0, r = A[worker];
while(l < r)
{
int mid = (l + r) / 2;
if(B[worker][mid] < color)
l = mid + 1;
else
r = mid;
}
return (l < A[worker] && B[worker][l] == color);
};
vector <vector <int>> maxr (2, vector <int> (M));
vector <bool> possible (N - M + 1, false);
for(int i = N - 1; i >= 0; i--)
for(int j : likers[C[i]])
{
int jnext = (j + 1) % M;
if(i == N - 1 || likes(jnext, C[i + 1]) == false)
maxr[i % 2][j] = i;
else
maxr[i % 2][j] = maxr[(i + 1) % 2][jnext];
if(i + M - 1 < N)
{
if(maxr[i % 2][j] <= i + M - 1)
possible[i] = true;
}
}
vector <int> minPref (N, INF);
deque <int> dq;
minPref[M - 1] = INF;
if(possible[0] == true)
minPref[M - 1] = 1;
for(int i = M; i < N; i++)
{
if(dq.empty() == false && dq.front() < i - M)
dq.pop_front();
while(dq.empty() == false && minPref[i - 1] <= minPref[dq.back()])
dq.pop_back();
dq.push_back(i - 1);
if(possible[i - M + 1] == true)
{
if(minPref[dq.front()] != INF)
minPref[i] = minPref[dq.front()] + 1;
}
}
if(minPref[N - 1] == INF)
return -1;
return minPref[N - 1];
}
# | 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... |