其他分享
首页 > 其他分享> > 洛谷 P4053 [JSOI2007]建筑抢修

洛谷 P4053 [JSOI2007]建筑抢修

作者:互联网

洛谷 P4053 [JSOI2007]建筑抢修

Description

Input

Output

Sample Input

4
100 200
200 1300
1000 1250
2000 3200

Sample Output

3

Data Size

题解:

#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;
}

标签:洛谷,int,抢修,修理,que,P4053,JSOI2007,include,建筑
来源: https://www.cnblogs.com/BigYellowDog/p/11620732.html