Description Input
The first behavior is two spaces separated by a positive integer n, m, which represents the number of cities traveled and the number of months spent traveling. Next n lines, where line I contains two spaces separated by integers AI and Bi,ai represent the city number where he went. Bi is 0 or 1, if bi=0 indicates that the city Ai has no small l want to go to the sights, if Bi=1 is the city Ai has small l want to go to the sights,
Ai 22 is different and has 1<=ai<=n, that is, {AI} is an arrangement of 1,2....N.
For example {2,1,3,4...N}
n<=500000,m<=200000
Output
T includes only one row, with m spaces separated by a positive integer x1,x2 ... Xm,t consists of only one row, with m spaces separated by a positive integer x1,x2 ... Xm, which corresponds to the itinerary of the travel plan arranged for small L. For the itinerary of the travel plan arranged for small L.
Sample INPUT8 3
2 0
3 1
4 1
1 0
5 0
6 1
7 1
: R
Sample OUTPUT1 6 8
HINT
The first 1 months to get 2 points of happiness and 2 points of fatigue value, the first 2 months to get 1 points of happiness and 1 points of fatigue value, 3 months to get 1 point Happy value with 1 point fatigue value. 3 months the maximum value of fatigue and happiness difference is 0, reaching all scheme minimums.
Possible options are:
1 6 8
3 6 8
3 1 8
where 1 6 8 is the smallest dictionary order.
Exercises
Array B[i] denotes city a[i] is there a small l want to go to the sights, 1 means there, 1 means no
Sum[i]=b[i+1]+b[i+2]+...+b[n],s=sum[0], the maximum value of the minimum is D, then
If s==0
The number of if Sum[i]==0 >= m will do the number equal to 0 to do the monotone queue again
else d=1
Else D=ceil (ABS (S)/m)
Proof See http://cxjyxx.me/?p=329
And then how to find the smallest dictionary order.
Every time we have to find a minimum number that can make the number of the back still have a solution as the end
This new interval is the first I interval and the end point is K
First, this new range is legal, then ABS (S-sum[k])/(m-i) <=d
Then make sure that there is a solution in the back, then Ceil (ABS (SUM[K)/(m-i)) <=d
The minimum value of each sum value is then maintained with a monotone queue
See Code for details
Code
1#include <cstdio>2#include <iostream>3#include <cmath>4#include <cstring>5#include <algorithm>6 #defineMAXN 5000057 #defineBase MAXN8 using namespacestd;9 Charch;Ten BOOLOK; One voidReadint&x) { A for(ok=0, Ch=getchar ();! IsDigit (CH); Ch=getchar ())if(ch=='-') ok=1; - for(x=0; isdigit (ch); x=x*Ten+ch-'0', ch=GetChar ()); - if(OK) x=-x; the } - structpoint{intV,id;}; - intCNT; - structnode{ + Point p; - intPre,next; +}t[maxn<<1]; A intNewNode (Point P,intPreintNext) {t[++cnt]= (node) {p,pre,next};returnCNT;} at structqueue{ - intSiz,head,tail; - BOOLEmpty () {return!siz;} - voidPush_front (point p) { - if(!siz) Head=tail=newnode (p,0,0); - ElseT[head].pre=newnode (P,0, head), head=T[head].pre; in++siz; - } to voidpush_back (point p) { + if(!siz) Head=tail=newnode (p,0,0); - ElseT[tail].next=newnode (P,tail,0), tail=T[tail].next; the++siz; * } $ voidPop_front () {siz--, head=T[head].next;}Panax Notoginseng voidPop_back () {siz--, tail=T[tail].pre;} -Point Front () {returnT[HEAD].P;} thePoint back () {returnT[TAIL].P;} +}list[maxn<<1]; A inttot,now[maxn<<1],pre[maxn<<1]; thePoint point[maxn<<1]; + intN,M,LAST,VAL[MAXN],SUM[MAXN],REST[MAXN]; - voidAddintU,point p) {pre[++tot]=now[u],now[u]=tot,point[tot]=p;} $ intCalc () { $ intans=0; - for(inti=n;i>=1; i--){ - if(!sum[i]) ans++,rest[i]=1; therest[i]+=rest[i+1]; - }Wuyi if(ans>=m)return 0;Else return 1; the } - voidPushintU,point P) { Wu while(!list[u].empty () &&p.v<List[u].back (). V) list[u].pop_back (); - List[u].push_back (p); About } $Point Calc (intUintLim) { - for(intP=now[u];p &&point[p].id<=lim;p=pre[p]) push (u,point[p]), now[u]=p; - while(!list[u].empty () &&list[u].front (). id<=Last ) List[u].pop_front (); - returnList[u].empty ()?(point) {Maxn,maxn}:list[u].front (); A } +Point min (point A,point b) {returnA.V<B.V?a:b;} the intMain () { - read (n), read (m); $ for(intI=1; i<=n;i++) read (Val[i]), read (sum[i-1]), sum[i-1]= (sum[i-1])?1:-1; the for(inti=n-1; i>=0; i--) sum[i]+=sum[i+1]; the for(inti=n;i>=1; i--) Add (sum[i]+Base, (point) {val[i],i}); the ints=sum[0],d= (s!=0)? (int) Ceil (1.0*abs (S)/(1.0*m)): Calc (); the if(d) { - for(intI=1; i<m;i++){ inPoint ans=(point) {MAXN,MAXN}; the for(intJ=s-d+Base; j<=s+d+Base; j + +){ the if(Ceil (ABS (1.0*j-Base)/(1.0*m-i)) <=d) AboutAns=min (Ans,calc (j,n-(M-i))); the } theprintf"%d", ANS.V); thelast=ans.id,s=Sum[last]; + } -printf"%d\n", Val[n]); the }Bayi Else{ the for(intI=1, p=now[Base];i<m;i++){ the for(;p &&rest[point[p].id]-1>=M-I;P=PRE[P]) Push (Base, Point[p]); -printf"%d", list[Base].front (). v); -list[Base].pop_front (); the } theprintf"%d\n", Val[n]); the } the return 0; -}
bzoj3141: [Hnoi2013] Travel