【习题 8-12 UVA - 1153】Keep the Customer Satisfied

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


在这里输入题意

【题解】


结束时间比较早的,就早点开始做。
所以,将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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值