[BZOJ1029] [JSOI2007] Construction Repair

Source: Internet
Author: User

1029: [JSOI2007] Building Repair time limit:4 Sec Memory limit:162 MB
submit:5041 solved:2242
[Submit] [Status] [discuss] 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 Output3This problem uses greedy thought, first for all the number of T2 sort, then maintenance repair of the T1 of the large heap, each looking for the largest heap in the current T1 is less than it, less than replace. Correctness obviously.
1 #include <iostream>2 #include <cstring>3 #include <cstdio>4 #include <cstdlib>5 #include <cmath>6 #include <algorithm>7 #include <queue>8 #include <vector>9 #define LL Long LongTen using namespace std; one int n; a struct Data{int t1,t2;}a[150005]; - BOOL CMP (data c1,data c2){return c1.t2<c2.t2;} - struct CMP1 the{ - bool operator () (data c1,data c2) {return c1.t1<c2.t1;} - }; - priority_queue<data,vector<data>,cmp1> q; + int main () -{ + scanf ("%d", &n); a for (int i=1;i<=n;i++) scanf ("%d%d", &a[i].t1,&a[i].t2); at LL sum=0; - int ans=0; - Sort (a+1,a+n+1,cmp); - for (int I=1;i<=n;i++) -     { - if (a[i].t2>=sum+a[i].t1) {q.push (a[i]); ans++;sum+=a[i].t1;} in Else -         { to if (q.empty ()) continue; + Data now=q.top (); - if (now.t1>a[i].t1) the             { * sum=sum-now.t1+a[i].t1; $ Q.pop ();Panax Notoginseng Q.push (a[i]); -             } the         } +     } a printf ("%d", ans); the }
View Code

[BZOJ1029] [JSOI2007] Construction Repair

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.