任务执行顺序

来源:互联网 发布:linux u盘 mknod 编辑:程序博客网 时间:2024/05/21 13:34
有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

#include <iostream>using namespace std;int main(){    int n;    cin >> n;    int a, b;    cin >> a >> b;    int minSpace = a-b;    int maxSpace = a-b;    long long int result = b;    int maxSum = a;        for (int i = 1; i < n; i++)    {        cin >> a >> b;        int temp = a-b;        if (temp < minSpace)        {            minSpace = temp;        }        else if (temp > maxSpace)        {            maxSpace = temp;            maxSum = a;        }                result += b;    }        result += minSpace;    if (result < maxSum)    {        result = maxSum;    }        cout << result << endl;    return 0;}