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 invaders of the Z tribe. But the base of the T tribe has been severely damaged by N construction facilities, which, if not repaired as soon as possible, will be completely destroyed. The situation today is that there is only one repairman at the base of the T tribe, and although he can get there in no time at all, it takes a while to repair every building.
At the same time, the repairman repaired a building to repair the next building, and could not repair multiple buildings at the same time. Suppose a building has not been completely repaired for some time. The building was scrapped. Your task is to help small just make a reasonable order of repair. To repair as many buildings as possible.
Input
The first line is an integer n, followed by n rows of two integers per line t1,t2 describes a building: repairing the building requires T1 seconds. Let's say it's not repaired within T2 seconds. The building was scrapped.
Output
Outputs an integer s. It means that we can repair s buildings at most. N < 150,000; T1 < T2 < Maxlongint
Sample Input4
100 200
200 1300
1000 1250
3200Sample Output3The puzzle : The greedy strategy is more obvious. We sort by T2 and start from scratch, assuming we can repair it. Repair, or in the previous repair of the building to find a T1 the largest. See if it will be better to replace it with the current building.
Assume that it is replaced by: The maximum value of the time to use the heap to optimize a bit better.
#include <iostream> #include <cstdio> #include <queue> #include <algorithm>using namespace std; int n,now,ans;struct use{ int last,end;} E[1000001];p riority_queue<int>q;bool cmp (use A,use b) {return a.end<b.end;} int main () { scanf ("%d", &n); for (int i=1;i<=n;i++) scanf ("%d%d", &e[i].last,&e[i].end); Sort (e+1,e+n+1,cmp); now=0; for (int i=1;i<=n;i++) if (now+e[i].last<=e[i].end) {Ans++;q.push (e[i].last); now+=e[i].last;} else { int x; if (!q.empty ()) { x=q.top (); if (x>e[i].last&&e[i].last+now<=e[i].end+x) { q.pop (); Now-=x-e[i].last; Q.push (e[i].last); }}} Cout<<ans;}?
"BZOJ1029" "JSOI2007" "Construction repair" "Greedy + heap"