UVA - 12563 Jin Ge Jin Qu hao (01背包)

Input
The first line contains the number of test cases T (T ≤ 100). Each test case begins with two positive
integers n, t (1 ≤ n ≤ 50, 1 ≤ t ≤ 109
), the number of candidate songs (BESIDES Jin Ge Jin Qu)
and the time left (in seconds). The next line contains n positive integers, the lengths of each song, in
seconds. Each length will be less than 3 minutes — I know that most songs are longer than 3 minutes.
But don’t forget that we could manually “cut” the song after we feel satisfied, before the song ends.
So here “length” actually means “length of the part that we want to sing”.
It is guaranteed that the sum of lengths of all songs (including Jin Ge Jin Qu) will be strictly larger
than t.
Output
For each test case, print the maximum number of songs (including Jin Ge Jin Qu), and the total lengths
of songs that you’ll sing.
Explanation:
In the first example, the best we can do is to sing the third song (80 seconds), then Jin Ge Jin Qu
for another 678 seconds.
In the second example, we sing the first two (30+69=99 seconds). Then we still have one second
left, so we can sing Jin Ge Jin Qu for extra 678 seconds. However, if we sing the first and third song
instead (30+70=100 seconds), the time is already up (since we only have 100 seconds in total), so we
can’t sing Jin Ge Jin Qu anymore!


Sample Input
2
3 100
60 70 80
3 100
30 69 70

Sample Output
Case 1: 2 758
Case 2: 3 777

 

#include <iostream>
#include <string.h>
using namespace std;
const int MAX = 50 * 60 * 3 + 1;
struct Node {
    int n, t;

    bool operator<(const Node &rhs) const {
        return n < rhs.n ||
                n == rhs.n && t < rhs.t;
    }
}d[MAX];

int main() {
    int n,cas,t,ti;
    cin >> cas;
    for (int cass = 1; cass <= cas; ++cass) {
        cin >> n >> t;
        memset(d, 0, sizeof(d));
        for (int i = 1; i <= n; ++i) {
            cin >> ti;
            for (int v = t; v > ti; --v) { //0,1背包,滚动数组
                Node tmp;
                tmp.n = d[v - ti].n + 1;
                tmp.t = d[v - ti].t + ti;
                if (d[v] < tmp)
                    d[v] = tmp;
            }
        }
        printf("Case %d: %d %d\n", cass, d[t].n + 1, d[t].t + 678); //最后金曲也要算进去
    }
}

 

posted @ 2017-12-10 20:16  少年啦飞驰  阅读(123)  评论(0编辑  收藏  举报