BZOJ-1029: [JSOI2007] Construction repair (greedy + heap optimization)

Source: Internet
Author: User

1029: [JSOI2007] Building repair time limit:4 Sec Memory limit:162 MB
submit:5137 solved:2278
[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 Output3HINT Source

PS: Learn from the problem (this seems to be the fastest I have in addition to 1001)

Puzzle: 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.

1#include"bits/stdc++.h"2 using namespacestd;3typedefLong LongLL;4 Const intmax=150005;5 intN,ans;6 structNode {7 LL x, y;8     BOOL operator< (ConstNode &tt)Const {9         returny<Tt.y;Ten     } One }stu[max]; APriority_queue <int>Q; - intMain () { -Freopen ("repair.in","R", stdin); theFreopen ("Repair.out","W", stdout); -     inti,j; -scanf"%d",&n); -      for(i=1; i<=n;i++){ +scanf"%lld%lld",&stu[i].x,&stu[i].y); -     } +Sort (stu+1, stu+n+1); ALL now=0; at      for(i=1; i<=n;i++){ -         if(now+stu[i].x<=stu[i].y) { -now+=stu[i].x; -ans++; - Q.push (stu[i].x); -         } in         Else{ -             if(!q.size ())Continue; toLL zt=q.top (); +             if(zt>stu[i].x && now+stu[i].x<=stu[i].y+ZT) { - Q.pop (); thenow-= (zt-stu[i].x); * Q.push (stu[i].x); $             }Panax Notoginseng         } -     } theprintf"%d", ans); +     return 0; A}

BZOJ-1029: [JSOI2007] Construction repair (greedy + heap optimization)

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.