Topic links
Monotonic Queue Optimization
A lifting point to find the maximum value of the distance from the first position of each color after this position.
Then find out the minimum value of each start result.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 Const intinf=2147483647;7 intn,k,sum,ans=inf,head[ $],next[1000005],v[1000005],a[1000005];8InlineintGetint ()9 {Ten intf=1, ret=0; One CharCh=GetChar (); A while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} - while(ch>='0'&&ch<='9') ret*=Ten, ret+=ch-'0', ch=GetChar (); - returnf==-1?-Ret:ret; the } - BOOLjsintx) - { - intmx=0; + for(intI=1; i<=k;i++) - { + while(v[head[i]]>x) A { at if(!next[head[i]])return 0; -head[i]=Next[head[i]]; - } - if(v[head[i]]<=x) Mx=max (mx,x-V[head[i]]); - } -ans=min (ans,mx); in return 1; - } to intMain () + { -N=getint (); k=getint (); the for(intI=1; i<=k;i++) * { $ intx=getint ();Panax Notoginseng for(intj=1; j<=x;j++) - { the inty=getint (); +v[++sum]=y,next[sum]=head[i],head[i]=sum,a[sum]=y; A } the } +Sort (A +1, a+sum+1); - for(intI=sum;i>0; i--) $ { $ if(a[i]!=a[i+1]) - if(!js (A[i])) Break; - } theprintf"%d", ans); - return 0;Wuyi}
---restore content ends---
Topic links
Monotonic Queue Optimization
A lifting point to find the maximum value of the distance from the first position of each color after this position.
Then find out the minimum value of each start result.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 Const intinf=2147483647;7 intn,k,sum,ans=inf,head[ $],next[1000005],v[1000005],a[1000005];8InlineintGetint ()9 {Ten intf=1, ret=0; One CharCh=GetChar (); A while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} - while(ch>='0'&&ch<='9') ret*=Ten, ret+=ch-'0', ch=GetChar (); - returnf==-1?-Ret:ret; the } - BOOLjsintx) - { - intmx=0; + for(intI=1; i<=k;i++) - { + while(v[head[i]]>x) A { at if(!next[head[i]])return 0; -head[i]=Next[head[i]]; - } - if(v[head[i]]<=x) Mx=max (mx,x-V[head[i]]); - } -ans=min (ans,mx); in return 1; - } to intMain () + { -N=getint (); k=getint (); the for(intI=1; i<=k;i++) * { $ intx=getint ();Panax Notoginseng for(intj=1; j<=x;j++) - { the inty=getint (); +v[++sum]=y,next[sum]=head[i],head[i]=sum,a[sum]=y; A } the } +Sort (A +1, a+sum+1); - for(intI=sum;i>0; i--) $ { $ if(a[i]!=a[i+1]) - if(!js (A[i])) Break; - } theprintf"%d", ans); - return 0;Wuyi}
bzoj1293 [SCOI2009] Birthday present