#pragma GCC optimize("O2,unroll-loops")
#include "paint.h"
#include<stdio.h>
#include<map>
#include<time.h>
#include<stdlib.h>
#define assert(x) if (!(x)) __builtin_trap()
int max(int i,int j){return i>j?i:j;}
int min(int i,int j){return i<j?i:j;}
void sort(int*a,int l,int r)
{
while(l<r)
{
int i=l,j=l,k=r,t,p=a[l+rand()%(r-l)];
while(j<k)
{
int c=a[j]-p;
if(c<0)t=a[i],a[i]=a[j],a[j]=t,++i,++j;
else if(!c)++j;
else t=a[--k],a[k]=a[j],a[j]=t;
}
sort(a,l,i);
l=k;
}
}
#include <vector>
#define NN 100000
#define MM 50000
#define KK 100000
/* max (NN, KK) */
#define NK 100000
void push(int**eh,int*eo,int i,int j)
{
int o=eo[i]++;
if(!o)eh[i]=(int*)malloc(2*sizeof**eh);
else if(!(o&o-1))eh[i]=(int*)realloc(eh[i],2*o*sizeof**eh);
eh[i][o]=j;
}
void push_(int**eh,int*eo,int j) { return push(eh,eo,0,j); }
int*eh[2],eo[2],*fh[NK],fo[NK],*ll,lo,dp[NN],*gh[2],go[2];
int get(int cc, int pp)
{
if(cc<0)return 0;
cc&=1;
int lb=-1,ub=eo[cc&1];
while(ub-lb>1)
{
int mid=lb+(ub-lb)/2;
if(eh[cc&1][mid]<=pp) lb=mid;
else ub=mid;
}
if(lb==-1||eh[cc&1][lb]!=pp)return 0;
return gh[cc&1][lb];
}
int counter2=0;
int minimumInstructions(
int N, int M, int K, std::vector<int> C,
std::vector<int> A, std::vector<std::vector<int>> B) {
//assert(++counter2==1);
srand(time(0));
for(int i=0;i<M;++i)
for(int j=0;j<A[i];++j)
push(fh,fo,B[i][j],i);
for(int i=0;i<N;++i)
{
free(eh[i&1]);eo[i&1]=0;eh[i&1]=0;
free(gh[i&1]);go[i&1]=0;gh[i&1]=0;
for(int j=0;j<fo[C[i]];++j)
push(eh,eo,i&1,fh[C[i]][j]); /* N * sqrt(4e5) ? ? */
sort(eh[i&1],0,eo[i&1]);
for(int yy,j=0;j<eo[i&1];++j)
{
push(gh,go,i&1,yy=min(M,1+get(i-1,(M+eh[i&1][j]-1)%M)));
if(yy==M)
push_(&ll,&lo,i-M+1);
}
}
sort(ll,0,lo);
for(int i=0;i<N;++i) dp[i]=1e9;
{
std::map<int,int>in={{1000000000,1}};
static int *rr[NN],ro[NN];
for(int i=0;i<N;++i)
ro[i]=0,rr[i]=0;
for(int i=0,j=0;i<N;++i)
{
while(j<lo&&ll[j]<=i)
{
int before=ll[j]==0?0:dp[ll[j]-1];
++in[before+1];
if(i+M<N)
push(rr,ro,i+M,before+1);
++j;
}
for(int j=0;j<ro[i];++j)
if(!--in[rr[i][j]])in.erase(rr[i][j]);
dp[i]=in.begin()->first;
}
for(int i=0;i<N;++i)
free(rr[i]),rr[i]=0;
}
for(int i=0;i<M;++i)
for(int j=0;j<A[i];++j)
fo[B[i][j]]=0,fh[B[i][j]]=0;
for(int i=0;i<M;++i)
eo[i]=fo[i]=0,free(eh[i]),free(fh[i]),eh[i]=fh[i]=0;
lo=0; free(ll),ll=0;
return dp[N-1] >= 1e9 ? -1 : dp[N-1];
}
Compilation message
paint.cpp: In function 'void push(int**, int*, int, int)':
paint.cpp:41:18: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
41 | else if(!(o&o-1))eh[i]=(int*)realloc(eh[i],2*o*sizeof**eh);
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Runtime error |
2 ms |
2652 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Runtime error |
2 ms |
2652 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Runtime error |
2 ms |
2652 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Runtime error |
2 ms |
2652 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Runtime error |
2 ms |
2652 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |