1099任务执行顺序
有N个任务需要执行,第i个任务计算时占R[i]个空间,而后会释放一部分,最后储存计算结果需要占据O[i]个空间(O[i] < R[i])。例如:执行需要5个空间,最后储存需要2个空间。给出N个任务执行和存储所需的空间,问执行所有任务最少需要多少空间
输入
第1行:1个数N,表示任务的数量。(2 <= N <= 100000)
第2 - N + 1行:每行2个数R[i]和O[i],分别为执行所需的空间和存储所需的空间。(1 <= O[i] < R[i] <= 10000)
输出
输出执行所有任务所需要的最少空间。
输入样例
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
输出样例
135
这个题没有什么好说的,单纯的贪心,首先进行运行和保存差距大的,其次执行运行大的,然后执行储存小的,权值一次减小,然后进行遍历就可以可,这里用优先队列比较好。
展示代码,不懂的可以看看贪心思想。
#include <stdio.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;
typedef long long LL;
const int Maxn = 1e5+100;
LL isold;
int n;
LL smax;
struct Node{
int run;
int old;
bool operator <(const Node &a)const{
if(run-old!=a.run-a.old)
return run-old<a.run-a.old;
if(run==a.run)
return old>a.old;
return run<a.run;
}
}tab[Maxn];
priority_queue<Node> que;
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++){
Node temp;
scanf("%d%d",&temp.run,&temp.old);
que.push(temp);
}
isold = 0;
smax = 0;
while(!que.empty()){
Node tp =que.top();
que.pop();
//printf("%d %d\n",tp.run,tp.old);
smax = max(smax,isold+tp.run);
isold+=tp.old;
}
printf("%I64d\n",smax);
return 0;
}