51Nod 1099 任务执行顺序

来源:互联网 发布:ubuntu黑客帝国屏保 编辑:程序博客网 时间:2024/04/28 00:18
有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示例
2014 12 111 320 47 56 520 719 89 420 1018 1112 613 1214 915 216 1517 1519 1320 220 1
Output示例
135

分析:排序的时候要将 oper-store 这个差值大的放前面,让它先执行,这样就能取到最小值。

#include <stdio.h>#include <algorithm>using namespace std;struct node{int store;int oper;}a[100005];bool cmp(node n1, node n2){  return n1.oper - n1.store > n2.oper - n2.store;}int main(){        int n;int sum = 0;scanf("%d", &n);        sum = 0;for (int i = 0; i < n; i++){scanf("%d%d", &a[i].oper, &a[i].store);sum += a[i].store;}sort(a, a + n, cmp);        int already = 0;int temp;for (int i = 0; i < n; i++){temp = already + a[i].oper;already += a[i].store;if(temp > sum)  sum = temp;}printf("%d\n", sum);return 0;}



0 0