第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 1Output示例135思路:
简单贪心,比较两个任务不同执行顺序结果的大小来定义任务先后,然后按优先级排序即可。代码:
#include <bits/stdc++.h>using namespace std;const int MAXN = 1e5 + 10;struct node { int x, y; bool Operator < (const node &rhs) const { return y + rhs.x < x + rhs.y; }}a[MAXN];int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d", &a[i].x, &a[i].y); } sort (a + 1, a + 1 + n); int now = 0, ans = 0; for (int i = 1; i <= n; i++) { ans = max(ans, now + a[i].x); now += a[i].y; } PRintf("%d/n", ans); return 0;}
新闻热点
疑难解答