Description
Xiao Gang is playing a computer game called "Building Repair" provided by Jsoi: After a fierce battle, the T tribe destroyed all the invaders of the Z tribe. But there are already n construction facilities in the T tribe's base that have been severely damaged, and if not repaired soon, the construction facilities will be completely destroyed. Now the situation is: there is only one repairman at the base of the T tribe, although he can get to any building in an instant, but repairing each building will take some time. At the same time, the repairman repaired a building to repair the next building, unable to repair multiple buildings at the same time. If a building has not been completely repaired for a period of time, the building will be scrapped. Your mission is to help small just make a reasonable order of repairs to repair as many buildings as possible.
Input
The first line is an integer n, and the next n rows of two integers per line t1,t2 describe a building: it takes T1 seconds to repair the building, and if it is not repaired within T2 seconds, the building is scrapped.
Output
Output an integer s, which means that a maximum of s buildings can be repaired. N < 150,000; T1 < T2 < Maxlongint
Sample Input4
100 200
200 1300
1000 1250
3200Sample Output3Sort by T2 and use a heap maintenance to choose the building to be repaired. After each join to determine whether the time is enough, not enough to give up the longest repair building.
#include <cstdio>#include<cctype>#include<queue>#include<cmath>#include<cstring>#include<algorithm>#defineRep (i,s,t) for (int i=s;i<=t;i++)#defineDwn (i,s,t) for (int i=s;i>=t;i--)#defineren for (int i=first[x];i;i=next[i])using namespacestd;Const intBuffersize=1<< -;Charbuffer[buffersize],*head,*Tail;inlineCharGetchar () {if(head==tail) { intL=fread (Buffer,1, Buffersize,stdin); Tail= (Head=buffer) +l; } return*head++;} InlineintRead () {intx=0, f=1;CharC=Getchar (); for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1; for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0'; returnx*F;}Const intmaxn=150010;structBuilding {intneed,t; BOOL operator< (Constbuilding& ths)Const{returnt<ths.t;}} A[MAXN];p riority_queue<int>Q;intMain () {intN=read (), t=0; Rep (I,1, n) a[i].need=read (), a[i].t=read (); Sort (A+1, a+n+1); Rep (I,1, N) {Q.push (a[i].need); t+=A[i].need; while(t>a[i].t) t-=q.top (), Q.pop (); } printf ("%d\n", Q.size ()); return 0;}View Code
BZOJ1029: [JSOI2007] Construction Repair