题解:P15699 [2018 KAIST RUN Spring] Touch The Sky

· · 题解

反悔贪心。

我们可以把上升的高度看作完成任务的所需时间,又发现最高海拔限制就是最晚开始任务时间,让它加上完成任务的时间就得出了最晚完成任务的时间。

发现是一眼的反悔贪心,把截止时间排个序,每次直接完成任务,如果时间超了,就把耗时最长的任务弹出。

:::success[code]

#include<bits/stdc++.h>
#define int long long

using namespace std;

const int N=1e7+5e6;

template<typename T>static inline void read(T&x) {
    char c;
    long long sign = 1;
    x = 0;
    do {
        c = getchar();
        if (c == '-') sign = -1;
    } while (!isdigit(c));
    do {
        x = (x<<1)+(x<<3)+(c&15);
        c = getchar();
    } while (isdigit(c));
    x *= sign;
}

static inline void write(int x) {
    static int sta[60];
    int top = 0;
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    do {
        sta[top++] = x % 10;
        x /= 10;
    } while (x);
    while (top) putchar('0' + sta[--top]);
    putchar('\n');
}

pair<int,int> a[N];

static inline void solve(){
    priority_queue<int> q;
    int n;
    read(n);
    for(int i=1;i<=n;i++){
        read(a[i].first);
        read(a[i].second);
        a[i].first+=a[i].second;
    }

    sort(a+1,a+1+n);

    int cnt=0;

    for(int i=1;i<=n;i++){
        cnt+=a[i].second;
        q.push(a[i].second);
        if(cnt>a[i].first)cnt-=q.top(),q.pop();
    }

    write((q.size()));
}

int32_t main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int t;
//  cin>>t;
    t=1;
    while(t--)solve();
    return 0;
}

:::