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 <= T <= 100
- 1 <= N <= 100 000
- 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;
}