[luogu4053 JSOI2007] construction repair (greedy priority queue)

Source: Internet
Author: User

Portal

Title 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/output format

Input format:

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 format:

Output an integer s, which means that a maximum of s buildings can be repaired.

Input/Output sample

Input Sample # #:

4
100 200
200 1300
1000 1250
2000 3200

Sample # # of output:

3

Description

N < 150,000; T1 < T2 < Maxlongint

Exercises

Light Press T2 Greedy obviously not, then should consider to solve the greedy failure situation
Apparently it's about the T1 problem--making a T1 smaller and obviously better (the same number of repairs takes less time)
Then use a priority queue to record the previous maximum T1 worth the current T2 does not go down and the current T1 is less than the largest before the T1 to change it in
And then I'm done. (~ ̄▽ ̄) ~

Code

//By Menteur_Hxy#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<cmath>#include<queue> #define LL long longusing namespace std;const int N=150010;int n;struct blo{    LL t1,t2;}B[N];bool cmp(blo a,blo b) {    return a.t2<b.t2;}priority_queue<int> Q;int main() {    scanf("%d",&n);    for(int i=1;i<=n;i++) scanf("%lld %lld",&B[i].t1,&B[i].t2);    sort(B+1,B+1+n,cmp);    LL now=0,ans=0;    for(int i=1;i<=n;i++) {        if(B[i].t2>=now+B[i].t1) ans++,now+=B[i].t1,Q.push(B[i].t1);        else {            LL ret=Q.top();            if(ret>B[i].t1) {                Q.pop();                Q.push(B[i].t1);                now-=ret-B[i].t1;            }        }    }    printf("%lld",ans);    return 0;}

[luogu4053 JSOI2007] construction repair (greedy priority queue)

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.