abc325D 打印机最多能打印的文档数

有n份文档和一台打印机,第i份文档在t[i]时刻进入打印区,停留d[i]时间后离开打印区,打印机可以在[t[i],t[i]+d[i]]范围内打印它,打印耗时不计,在打印完成后,需要1个单位时间恢复。问最多能打印多少份材料?
1<=n<=2e5; 1<=t[i],d[i]<=1e18

打印机每次应选择在打印区内,并且最先离开打印区的那份打印。

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=b;i>=a;i--)

const int N = 200005;
int n;
void solve() {
    cin >> n;
    vector<pair<int,int>> vp(n);
    rep(i,0,n-1) {
        int x, y;
        cin >> x >> y;
        vp[i] = {x, x+y};
    }
    sort(vp.begin(), vp.end());

    int ans = 0;
    priority_queue<int,vector<int>,greater<int>> q;
    for (int t = 0, i = 0; i < n || !q.empty(); t++) {
        if (q.empty()) {
            t = vp[i].first;
        }
        while (i < n && vp[i].first == t) {
            q.push(vp[i].second);
            i += 1;
        }
        while (!q.empty() && q.top() < t) {
            q.pop();
        }
        if (!q.empty()) {
            ans += 1;
            q.pop();
        }
    }
    cout << ans << "\n";
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    while (t--) solve();
    return 0;
}
posted @ 2024-03-17 19:15  chenfy27  阅读(8)  评论(0编辑  收藏  举报