1029: [JSOI2007] Construction Repair
Description
Xiao Gang is playing a computer game called "Building Repair" provided by Jsoi: After a fierce battle, the T tribe wiped out all the Z tribes
Intruders. 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
Destroy. 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 requires
To a certain 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 is in a
The building was scrapped when it was not completely repaired within a period of time. Your task is to help small just rationalize a repair sequence to repair as much
of buildings.
Input
The first line is an integer n the next n rows of two integers per line t1,t2 describe a building: repairing the building takes T1 seconds, and if it's within T2 seconds
Without the repair, the building was 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 Output3Exercises
First we sort the T2 and then fix it in turn, but this greed is obviously not correct.
We maintain a large pile, and every building we repair, we put the T1 value of the building into the heap. If we can't fix it now, we can judge if the top of the heap is bigger than the T1 of this building. To repair the current building instead of repairing the top of the heap
This does not update ans but can save time for the building behind
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#include<queue>using namespacestd;Const intN = 2e5+Ten, inf = 2e9, mod = 1e9+7; typedefLong Longll;intN;pair<int,int>P[n];intMain () {scanf ("%d",&N); for(intI=1; i<=n;i++) {scanf ("%d%d",&p[i].second,&P[i].first); } intAns =0; LL TMP=0; Sort (P+1, p+n+1); Priority_queue<int>Q; for(intI=1; i<=n;i++) { if(p[i].first>=p[i].second+tmp) Q.push (p[i].second), ans++,tmp+=P[i].second; Else { if(Q.empty ())Continue; intK =Q.top (); if(k>P[i].second) {tmp-=K; TMP+=P[i].second; Q.pop (); Q.push (P[i].second); }}} printf ("%d\n", ans); return 0;}
Bzoj 1029: [JSOI2007] Construction repair heap + greed