# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
566349 | zaneyu | Painting Walls (APIO20_paint) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
8 3 5
3 3 1 3 4 4 2 2
3 0 1 2
2 2 3
2 3 4
*/
#include "paint_apio.h"
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define ALL(x) x.begin(),x.end()
#define pii pair<int,int>
#define f first
#define s second
#define MXTO(a,b) a=max(a,b)
#define MNTO(a,b) a=min(a,b)
#define sz(x) (int)x.size()
#define pb push_back
const int maxn=1e5+5;
int n,m,k;
int dp[maxn];
vector<int> ok[maxn];
const int INF=0x3f3f3f3f;
vector<int> v[maxn];
int lp[maxn],rp[maxn];
mt19937 rng(69);
int md[maxn];
int minimumInstructions(
int N, int M, int K, std::vector<int> C,
std::vector<int> A, std::vector<std::vector<int>> B) {
C.emplace(C.begin(),0);
n=N,m=M,k=K;
multiset<int> ms;
ms.insert(0);
REP(i,m){
for(int x:B[i]) ok[x].pb(i);
}
REP1(i,k) shuffle(ALL(ok[i]),rng);
REP1(i,n) md[i]=(i%m);
REP1(i,n){
for(int x:ok[C[i]]){
int z=(x-md[i]+m);
if(z>=m) z-=m;
v[z].pb(i);
}
}
deque<int> dq;
dq.pb(0);
REP1(i,m-1) dp[i]=INF,dq.pb(i);
for(int i=m;i<=n;i++){
while(sz(dq) and dq.front()<i-m) dq.pop_front();
int mn=dp[dq.front()];
dp[i]=INF;
vector<int> ans;
for(int a:ok[C[i]]){
int z=(a-md[i]+m);
if(z>=m) z-=m;
while(rp[z]<sz(v[z]) and v[z][rp[z]]<=i) ++rp[z];
while(lp[z]<sz(v[z]) and v[z][lp[z]]<i-m+1) ++lp[z];
if(rp[z]-lp[z]==m){
dp[i]=mn+1;
break;
}
}
while(sz(dq) and dp[dq.back()]>dp[i]) dq.pop_back();
dq.pb(i);
}
if(dp[n]>=INF) dp[n]=-1;
return dp[n];
}