51 nod 1099 任务执行顺序

基准时间限制:1 秒 空间限制:131072 KB 分值: 20  难度:3级算法题
 收藏
 关注
有N个任务需要执行,第i个任务计算时占R[i]个空间,而后会释放一部分,最后储存计算结果需要占据O[i]个空间(O[i] < R[i])。
例如:执行需要5个空间,最后储存需要2个空间。给出N个任务执行和存储所需的空间,问执行所有任务最少需要多少空间。
Input
第1行:1个数N,表示任务的数量。(2 <= N <= 100000)
第2 - N + 1行:每行2个数R[i]和O[i],分别为执行所需的空间和存储所需的空间。(1 <= O[i] < R[i] <= 10000)
Output
输出执行所有任务所需要的最少空间。
Input示例
20
14 1
2 1
11 3
20 4
7 5
6 5
20 7
19 8
9 4
20 10
18 11
12 6
13 12
14 9
15 2
16 15
17 15
19 13
20 2
20 1
Output示例
135
#include <iostream>
#include <algorithm>
using LL = long long;
using namespace std;
struct task
{
	int room;
	int store;
}a[100005];
bool cmp(task a, task b)
{
	if(a.room-a.store == b.room-b.store)
	return a.room>b.room;
	return a.room-a.store>b.room-b.store;
}
int main()
{
	int n,i;
	cin>>n;
	for(i=0; i<n; ++i)
	{
		cin>>a[i].room>>a[i].store;
	}
	sort(a,a+n,cmp);
	LL Room = a[0].store,
	   maxroom=a[0].room;
	for(i=1; i<n; ++i)
	{
		if(a[i].room+Room>maxroom)
		maxroom=a[i].room+Room;
		Room+=a[i].store;
	}
	cout<<maxroom<<endl;
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值