"BZOJ1029" "JSOI2007" "Construction repair" "Greedy + heap"

Source: Internet
Author: User

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 Output3Puzzle : Greedy strategy Comparison Obviously, we sort by T2, and then start from scratch, if you can repair, repair, or in the previous repair of the building to find a T1 the largest, look at the current building instead of it will be more excellent. If it is, replace it. 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;}?


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"BZOJ1029" "JSOI2007" "Construction repair" "Greedy + heap"

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.