Portal 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 Output3 Solving
Realize that this is a very 2 question ...
We consider that if the building is repaired before it is scrapped, the results will be the same whenever it is repaired. So we sort all the data according to the scrap time, using the priority queue to maintain the large-to-small sort order of the currently selected answers. If it can then be completed within the specified time, it will be inserted directly into the priority queue, otherwise the repair time is compared with the maximum value in the priority queue, if the maximum value is smaller than the maximum value of the building to repair the building will be better. Because we sort by the completion time, we don't have to worry about whether we'll be out of time.
Code
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <algorithm>6#include <cmath>7#include <vector>8#include <queue>9 using namespacestd;Tenpriority_queue<int>PQ; One structnode{ A intRe,ot; -}a[150010]; - intN,now,ans; the BOOLCMP (node A,node b) { - returna.ot<B.ot; - } - intMain () { +scanf"%d",&n); - inti,j; + for(i=1; i<=n;++i) { Ascanf"%d%d",&a[i].re,&a[i].ot); at } -Sort (A +1, A +1+n,cmp); -now=0; ans=0; - for(i=1; i<=n;++i) { - if(a[i].re+now<=A[i].ot) { -ans++;now+=a[i].re; in Pq.push (a[i].re); - } to Else{ + inttot=pq.top (); - if(a[i].re<tot) { the Pq.pop ();p Q.push (a[i].re); *now=now-tot+a[i].re; $ }Panax Notoginseng } - } theprintf"%d\n", ans); + return 0; A}
BZOJ1029[JSOI2007] Construction Repair