As you are a contestant participating in an ACM-ICPC contest, your goal is to solve as many problems
as you can within the competition duration with the least total time possible.
Let’s suppose that you can, in advance, estimate the time (in minutes) that you will spend to solve
each problem. Your task is to plan how to solve the contest problems that can maximize the number of
solved problems while minimize the total time. The total time is the sum of the time (in minutes) for
each problem solved at the submission time which is the elapsed time (in minutes) from the start of the
contest. In your plan, at best you can assume that you can solve every problem at the first submission
on the estimation time, so you do not have to worry about the penalty time. You can only work on 1
problem at any time (no parallel work).
Input
The first line contains integer T (1 ≤ T ≤ 20) which is the number of test cases. Each test case has
2 lines. The first line of each test case contains 2 integers: N (1 ≤ N ≤ 20) is the number of contest
problems and L (1 ≤ L ≤ 1, 500) is the duration of the contest (time in minutes). Then the second line
contains N integers indicating the estimation time for each contest problem.
Output
For each test case, print out the case number followed by your best result possible containing 3 integers.
The first value is the number of solved problems. The second value is the time for your last solved
problem and the third value is the total time. See the samples for the exact format of output.
Explanation:
For the first test case in the sample input, the contest has 6 problems (A-F) and the competition
lasts 100 minutes. The times that you will use for each problem are given in the second line, i.e.
Problem A uses15 minutes, Problem B uses 23 minutes, Problem C uses 41 minutes and so on.
For the best score in this particular contest, you will solve at most 5 problems. The last problem
that you can solve is submitted at 85 minutes and will have the total time of 228. You will not have
time to solve problem C.
Sample Input
2
6 100
15 23 41 12 15 20
5 200
23 45 35 49 28
Sample Output
Case 1: 5 85 228
as you can within the competition duration with the least total time possible.
Let’s suppose that you can, in advance, estimate the time (in minutes) that you will spend to solve
each problem. Your task is to plan how to solve the contest problems that can maximize the number of
solved problems while minimize the total time. The total time is the sum of the time (in minutes) for
each problem solved at the submission time which is the elapsed time (in minutes) from the start of the
contest. In your plan, at best you can assume that you can solve every problem at the first submission
on the estimation time, so you do not have to worry about the penalty time. You can only work on 1
problem at any time (no parallel work).
Input
The first line contains integer T (1 ≤ T ≤ 20) which is the number of test cases. Each test case has
2 lines. The first line of each test case contains 2 integers: N (1 ≤ N ≤ 20) is the number of contest
problems and L (1 ≤ L ≤ 1, 500) is the duration of the contest (time in minutes). Then the second line
contains N integers indicating the estimation time for each contest problem.
Output
For each test case, print out the case number followed by your best result possible containing 3 integers.
The first value is the number of solved problems. The second value is the time for your last solved
problem and the third value is the total time. See the samples for the exact format of output.
Explanation:
For the first test case in the sample input, the contest has 6 problems (A-F) and the competition
lasts 100 minutes. The times that you will use for each problem are given in the second line, i.e.
Problem A uses15 minutes, Problem B uses 23 minutes, Problem C uses 41 minutes and so on.
For the best score in this particular contest, you will solve at most 5 problems. The last problem
that you can solve is submitted at 85 minutes and will have the total time of 228. You will not have
time to solve problem C.
Sample Input
2
6 100
15 23 41 12 15 20
5 200
23 45 35 49 28
Sample Output
Case 1: 5 85 228
Case 2: 5 180 471
思路:贪心+sort(起初就是因为sort使用不规范,导致半个小时没有解决,自己略坑啊)
#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int a[1550];
int main()
{
int T,N,L;
scanf("%d", &T);
for (int cas = 1; cas <= T; cas++)
{
scanf("%d%d", &N, &L);
for (int i = 0; i < N; i++)
scanf("%d", &a[i]);
sort(a, a + N);//从小到大排序
int cnt,tot1, tot2;//此题int范围不会溢出
cnt = tot1 = tot2 = 0;
for (int i = 0; i < N; i++) {
tot1 += a[i], tot2 += tot1, cnt++;
if (tot1 > L) {
cnt--, tot2 -= tot1, tot1 -= a[i];//超过总时间时说明正好能AC前i-1道题,那么就从tot2,tot1中减去第i题用时。
break;
}
}
printf("Case %d: %d %d %d\n", cas, cnt, tot1, tot2);
}
}