1029: [JSOI2007] Construction repair time limit: 4 Sec Memory limit: 162 MB
Submitted by: 3695 Solution: 1706 Title 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 output3The following:greedy, first T2 from small to large, and then began to choose. The choice will appear two kinds of situations, one can be repaired, one is not repair. can be repaired directly to calculate the situation, and the time accumulated. It is not possible to repair the situation, look at the time of repairing the building and the time before repairing the other buildings, if more than the previous time to repair the building, it means that there is a better way to exist. This will replace the one with the longest repair time to repair the building. concrete implementation with heap maintenance.
#include <iostream>#include<algorithm>#defineMAX 150000#definell Long Longusing namespacestd;structbuilding{ll T1; ll T2;} Building[max], Heap[max];intcmpConstBuilding & A,ConstBuilding &b) { if(A.t2! = b.t2)returnA.t2 <b.t2; returnA.t1 <b.t1;}intHEAPCMP (ConstBuilding & A,ConstBuilding &b) { returnA.t1 <b.t1;}intMain () {intN, tot =0; ll ans=0; CIN>>N; for(inti =1; I <= N; ++i) Cin>> Building[i]. T1 >>Building[i]. T2; Sort (Building+1, Building + N +1, CMP); for(inti =1; I <= N; ++i) {if(ans + building[i]. T1 <=Building[i]. T2) {heap[++tot] =Building[i]; Push_heap (Heap+1, Heap +1+tot, heapcmp); Ans= ans +Building[i]. T1; } Else if(Tot! =0) {Building step= heap[1]; if(Building[i]. T1 >= step. T1)Continue; Pop_heap (Heap+1, Heap +1+tot, heapcmp); Heap[tot]=Building[i]; Push_heap (Heap+1, Heap +1+tot, heapcmp); Ans= ans + building[i]. T1-step. T1; }} cout<<tot; return 0;}
Sugar
[JSOI2007] Construction Repair