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 about130012503200
Sample Output
3
=============================================================================================================== =================
Greedy thinking, by sorting, let the first scrapped buildings first repaired. and press the building's repair time into the priority queue
If the current time conflicts with the scrap time of the building. Compared with the maximum time of the current queue, the optimal time is guaranteed.
Therefore, the use of priority queue Priority_queue can be quickly compared.
=============================================================================================================== =================
Code:
#include <bits/stdc++.h>using namespacestd;Const intN = 15e4 +Ten;structnode{intT1,t2;} Node[n];BOOLCMP (Node A,node b) {returna.t2<B.t2;} Priority_queue<int>que;intMain () {intN scanf"%d",&N); for(inti =1; I <= n;++i) scanf ("%d%d",&node[i].t1,&node[i].t2); Sort (Node+1, node+n+1, CMP); intTime =0, sum =0; for(inti =1; I <= n;++i) {//Current time + time required for the building <= the building's scrap time if(time+node[i].t1<=node[i].t2) { time+=node[i].t1; //the time is pressed into the priority queueQue.push (NODE[I].T1); ++sum; } //if the above conditions are not met and the time for repair of the building is less than the longest time in the priority queue//and minus the maximum plus the current time can <= the building scrap time Else if(Node[i].t1<que.top () &&time-que.top () +node[i].t1<=node[i].t2) { time= Time-que.top () +node[i].t1; Que.pop (); Que.push (NODE[I].T1); }} printf ("%d\n", sum); return 0;}
[JSOI2007] Construction repair (greedy + priority queue)