知识点:贪心
根据题目的条件分析,同一时间只能交代一个任务,但是可以执行多个任务,这样,我们可以想象出,所有任务交代完的时间是一定的,在最短的要求下,那么执行时间长的任务早点交代,它就可以去执行了,和别的正在交代的任务时间是重叠的,节省时间,所以第一步的排序就是按照执行时间从大到小排序,如果执行时间相同呢,可以分析出,对于中间任意两个相邻的任务,改变其次序,不影响其它任务的执行结束时间,这都要归因于同一时间只能交代一个任务,交代完任务之后所有的任务就都去自己执行了,所以我们排序的第二标尺就是中间相邻的两个,哪个在前面结束时间短哪个就在前面,这样排序函数就可以写出来了
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
bool cmp(pair<int, int> a, pair<int, int> b) {
if (a.second != b.second) return a.second > b.second;
return a.first + b.first + b.second < b.first + a.first + a.second;
}
int main() {
int n;
int T = 1;
while (cin >> n && n) {
cout << "Case " << T++ << ": ";
pair<int, int> a[N];
for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second;
sort(a, a + n, cmp);
int tot = 0;
int rec = 0;
for (int i = 0; i < n; i++) {
tot += a[i].first;
rec = max(tot + a[i].second, rec);
}
cout << rec << endl;
}
return 0;
}