Luo Gu P4053 [JSOI2007] building repairs

Luo Gu P4053 [JSOI2007] building repairs

Description

  • Xiaogang in a play called "building repairs" computer game JSOI offer: After a fierce battle, T tribal invaders destroyed all z tribes. But T tribal base, already has N building facilities suffered serious damage, if not repaired, then as soon as possible, these facilities will be completely destroyed building. The current situation is: T tribal base, only a repairman, though he can instantly reach any building, but the building needs some repair each time. At the same time, complete repair repairman next to a building repair a building, multiple buildings can not be repaired. If a building is not fully repair completed within a period of time, the building will be scrapped. Your task is to help formulate a reasonable Xiaogang repair order to repair as much of the building.

Input

  • The first row is an integer N, the next two integers N lines T1, T2 is described a building: this construction needs T1 seconds repair, if repair is not yet completed within T2 seconds, the building scrapped.

Output

  • An output integer S, S represents a maximum repair buildings.

Sample Input

4
100 200
200 1300
1000 1250
2000 3200

Sample Output

3

Data Size

  • N < 150,000; T1 < T2 < maxlongint

answer:

  • Greedy + binary heap.
  • When writing this question, I thought of this question .
  • So for this problem, the same can be sorted according to the expiration time, in order to take the construction.
  • If you can give it to repair the repair, if not, take a look at the top of the heap if the most time-consuming project is greater than the current project, if it is greater than replaced, and then updated by the elapsed time. Or less on the matter.
  • It turns out not to be, but I have to think jiao very logical.
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#define N 150005
using namespace std;

struct A {int val, end;} a[N];
int n, sum, ans;
priority_queue<int> que;

int read()
{
    int x = 0; char c = getchar();
    while(c < '0' || c > '9') c = getchar();
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
    return x;
}

bool cmp(A x, A y) {return x.end < y.end;}

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
        a[i].val = read(), a[i].end = read();
    sort(a + 1, a + 1 + n, cmp);
    for(int i = 1; i <= n; i++)
        if(sum + a[i].val <= a[i].end)
            ans++, sum += a[i].val, que.push(a[i].val);
        else if(que.top() > a[i].val)
        {
            sum -= (que.top() - a[i].val);
            que.pop();
            que.push(a[i].val);
        }
    cout << ans;
    return 0;
}

Guess you like

Origin www.cnblogs.com/BigYellowDog/p/11620732.html