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 <= 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
#include
#include
#include
#include
#include
#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;
}
推荐阅读
- 数据结构和算法|LeetCode 的正确使用方式
- #|7.分布式事务管理
- 刷题记录|【蓝桥必胜】试题 算法训练 kAc给糖果你吃-贪心排序
- #|算法设计与分析(Java实现)——贪心算法(集合覆盖案例)
- #|算法设计与分析(Java实现)—— 动态规划 (0-1 背包问题)
- 动态规划|暴力递归经典问题
- #|阿尔法点亮LED灯(一)汇编语言
- #|Multimedia
- #|ARM裸机开发(汇编LED灯实验(I.MX6UL芯片))
- 基础课|使用深度优先搜索(DFS)、广度优先搜索(BFS)、A* 搜索算法求解 (n^2 -1) 数码难题,耗时与内存占用(时空复杂度)对比(附((n^2 - 1) 数码问题控