D - 淡黄的长裙 HDU - 4221(贪心)

D - 淡黄的长裙 HDU - 4221(贪心)

James is almost mad! Currently, he was assigned a lot of works to do,
so many that you can’t imagine. Each task costs Ai time as least, and
the worst news is, he must do this work no later than time Bi!

OMG, how could it be conceivable! After simple estimation, he
discovers a fact that if a work is finished after Bi, says Ti, he will
get a penalty Ti - Bi. Though it may be impossible for him to finish
every task before its deadline, he wants the maximum penalty of all
the tasks to be as small as possible. He can finish those tasks at any
order, and once a task begins, it can’t be interrupted. All tasks
should begin at integral times, and time begins from 0. Input The
first line contains a single integer T, indicating the number of test
cases. Each test case includes an integer N. Then N lines following,
each line contains two integers Ai and Bi.

Technical Specification

  1. 1 <= T <= 100
  2. 1 <= N <= 100 000
  3. 1 <= Ai, Bi <= 1 000 000 000 Output For each test case, output the case number first, then the smallest maximum penalty.
Sample Input
2
2
3 4
2 2
4
3 6
2 7
4 5
3 9
Sample Output
Case 1: 1
Case 2: 3

思路

  • 贪心 + 结构排序

代码

#include<iostream>
#include<cmath>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

const int Len = 100005;
struct Node
{
    ll s, e;
    bool operator < (const Node b) const 
    {
        if(e == b.e)
            return s < b.s;
        return e < b.e;
    }
} node[Len];


int main()
{
    /* freopen("A.txt","r",stdin); */
    int t, Case = 1;
    scanf("%d", &t);
    while(t --)
    {
        ll n;
        scanf("%lld", &n);
        for(int i = 1; i <= n; i ++)
            scanf("%lld %lld", &node[i].s, &node[i].e);
        sort(node + 1, node + 1 + n);
        ll ans = 0;
        ll sum = 0;

        for(int i = 1; i <= n; i ++)
        {
            sum += node[i].s;
            if(sum > node[i].e)
                ans = max(ans, sum - node[i].e);
        }
        printf("Case %d: %lld\n", Case ++, ans);
    }

    return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值