Bzoj 1029: [JSOI2007] Construction repair (greed)

Source: Internet
Author: User

Sort by T2 then greedy ...

----------------------------------------------------------------------------

#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int MAXN = 150009;struct O {int t1, T2;void Read () {scanf ("%d%d", &t1, &t2);}BOOL Operator < (const O &o) Const {return T2 < o.t2;}} O[MAXN];int N;priority_queue<int> Q;void init () {scanf ("%d", &n);for (int i = 0; i < N; i++) o[i]. Read ();} void Solve () {sort (o, O + N);int cnt = 0, ans = 0;for (int i = 0; i < N; i++) {if (cnt + o[i].t1 <= o[i].t2)cnt + = O[i].t1, Q.push (O[I].T1), ans++;else {int x = Q.top ();if (x > O[i].t1 && cnt-x + o[i].t1 <= o[i].t2)Q.pop (), Q.push (O[I].T1), cnt + = O[i].t1-x;}}printf ("%d\n", ans);} int main () {init ();solve ();return 0;}

---------------------------------------------------------------------------

1029: [JSOI2007] Construction repair time limit: 4 Sec Memory Limit: 162 MB
Submit: 3033 Solved: 1355
[Submit] [Status] [Discuss] 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.

Input

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

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 Output3HINT

Source

Bzoj 1029: [JSOI2007] Construction repair (greed)

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.