【习题 8-12 UVA

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

结束时间比较早的,就早点开始做。 所以,将n件事情,按照结束时间升序排。 然后对于第i件事情。 尽量把它往左排。 即t+1..t+a[i].q 然后如果发现t+a[i].q<=a[i].d了 那么前i件事情就不能全都做了。 那么我们尝试在之前的事情里面。 找一件事情的时长最大的事情。 然后看看这个最大时长是否大于a[i].q 如果是的话。就把那件事情换成这第i件事情。 做的事情的总数不变,但是总时长变短了。 这就让后面的事情有更大的机会被做了。 用priority_queue来维护最大时长的事情的时长就可以了。

【代码】

#include <bits/stdc++.h>
using namespace std;

const int N = 8e5;

struct abc{
    int q,e;
    bool operator < (const abc &b) const {
        return e < b.e;
    }
}a[N+10];

int n;
priority_queue <int,vector<int>,less<int> >pq;

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("rush_in.txt", "r", stdin);
	#endif
	ios::sync_with_stdio(0),cin.tie(0);

	int T;
	cin >> T;
	int kast = 0;
	while (T--){
	    if (kast>0) cout << endl;
        kast++;
        while (!pq.empty()) pq.pop();
        cin >> n;
        for (int i = 1;i <= n;i++){
            cin >> a[i].q >> a[i].e;
        }
        sort(a+1,a+1+n);
        int t = 0;
        for (int i = 1;i <= n;i++){
            if (t+a[i].q<=a[i].e){
                t+=a[i].q;
                pq.push(a[i].q);
            }else{
                int temp = pq.top();
                if (temp>a[i].q){
                    t-=(temp-a[i].q);
                    pq.pop();
                    pq.push(a[i].q);
                }
            }
        }
        cout << (int) pq.size()<<endl;
	}

	return 0;
}

原文地址:https://www.cnblogs.com/AWCXV/p/8268213.html