Jin Ge Jin Qu[01背包]

    xiaoxiao2021-03-25  147

    (If you smiled when you see the title, this problem is for you ^_^) For those who don‘t know KTV, see: http://en.wikipedia.org/wiki/Karaoke_box There is one very popular song called Jin Ge Jin Qu(劲歌金曲). It is a mix of 37 songs, and is extremely long (11 minutes and 18 seconds)[1]. Why is it popular? Suppose you have only 15 seconds left (until your time is up), then you should select another song as soon as possible, because the KTV will not crudely stop a song before it ends (people will get frustrated if it does so!). If you select a 2-minute song, you actually get 105 extra seconds! ....and if you select Jin Ge Jin Qu, you‘ll get 663 extra seconds!!! Now that you still have some time, but you‘d like to make a plan now. You should stick to the following rules: Don‘t sing a song more than once (including Jin Ge Jin Qu). For each song of length t, either sing it for exactly t seconds, or don‘t sing it at all. When a song is finished, always immediately start a new song. Your goal is simple: sing as many songs as possible, and leave KTV as late as possible (since we have rule 3, this also maximizes the total lengths of all songs we sing) when there are ties.

    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[2]. 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.

    Sample Input

    2 3 100 60 70 80 3 100 30 69 70 Output for the Sample Input Case 1: 2 758 Case 2: 3 777 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int main() { int cases; cin >> cases; const int inf = 0x3f3f3f3f; // for(int c = 1; c <= cases; c++){ int n , t; int song[100],f[100000]; cin >> n >> t; for(int i = 0 ; i < n; i++){ cin >> song[i]; } memset(f, -1, sizeof(f)); //这个memset 里面只能是0 -1 或者其他16进制的数 这里之所以设置成-1。这里考虑到刚好装满的情况要把dp数组全给设置成-inf //(song, 0, sizeof(song)); f[0] = 0; //只有当放满的时候就是剩余体积为0时 进行状态转移。 for(int i = 0; i < n; i++){ //很常规的01背包 for(int j = t - 1; j >= song[i]; j--){ f[j] = max(f[j], f[j-song[i]] + 1); } } int ans = t-1; for(int i = t-1; i >= 0; i--){ if(f[ans] < f[i]) { ans = i; } } cout << "Case " << c << ": " << f[ans]+1 << " " << ans + 678 << endl; // printf("%d",0x8f); } return 0; } /* 2 3 100 60 70 80 3 100 30 69 70 */
    转载请注明原文地址: https://ju.6miu.com/read-24602.html

    最新回复(0)