Title Description
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.
Enter a description
Input Description
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 description
Output Description
Output an integer s, which means that a maximum of s buildings can be repaired.
Sample input
Sample Input
4
100 200
200 1300
1000 1250
2000 3200
Sample output
Sample Output
3
Data range and Tips
Data Size & Hint
n<150000,t<=15000
The data is not the original data, it is my own data. The range of T is also different from the original problem.
is a greedy problem, discerning eye can see, unexpectedly this problem will appear in the Jsoi province election, my code is more than 30 lines, really cool! But this greedy to use a large heap to maintain the optimal solution, first of all the buildings by their vanishing time in ascending order (the more the first to disappear building first rescue), with the variable nowtime record the current time, from the first to the last building judgment to fix it, Whether it will exceed its vanishing time (so that the building after the rescue), if so, it is necessary to find the first building in the big pile, the repaired building is the most time-consuming, if the team first building repair time than the building, and if not repair team first building, can also save the building, Then update the current time, remove (not repair) the first building, and then the construction team
After all the buildings have been traversed, the number of elements in the large root heap is the largest number of buildings that can be rescued.
#include <stdio.h> #include <algorithm> #include <queue> #define MAXN 170000using namespace Std;struct T{int t1,t2;friend bool operator< (T a,t b) {return a.t2<b.t2;}//Sort with}A[MAXN];p riority_queue<int>q; Dagen Q Saves the time required for the individual repair of the repaired building int main () {int i,j,n,nowtime=0;//nowtime= Current time scanf ("%d", &n); for (i=1;i<=n;i++) scanf ("%d%d", &a[i].t1,&a[i].t2); sort (a+1,a+n+1); Sort by end time in ascending order for (i=1;i<=n;i++) {if (NOWTIME+A[I].T1<=A[I].T2)//If the time after completion of the first building is longer than the first building vanishing time {nowtime+=a[i].t1;// The current time plus the repair time required for the construction of Q.push (A[I].T1); To queue the repair time required for the building I construction}//Otherwise, the first building will not be repaired before it disappears, then it is necessary to decide whether to give up the previous repaired building, make time to repair the building else if (Nowtime+a[i].t1-q.top () <=a[i]. T2&&a[i].t1<=q.top ())//After the most time-consuming building, the building can be repaired, and the construction I time is less than that building, that will give up the most time-consuming building {Nowtime=nowtime+a[i].t1-q.top (); Q.pop (); Q.push (A[I].T1);}} printf ("%d\n", Q.size ()); The number of elements in the final root heap is the maximum number of buildings to be rescued return 0;}