UVaOJ 12563 Jin Ge Jin Qu hao(01背包)

本文介绍了一种在有限时间内选择KTV歌曲以最大化歌唱时间和歌曲数量的算法。通过01背包问题的应用,实现对歌曲的选择优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(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 extremelylong (11 minutes and 18 seconds) — I know that there are Jin Ge Jin Qu II and III, and some otherunofficial versions. But in this problem please forget about them.

Why is it popular? Suppose you have only 15 seconds left (until your time is up), then you shouldselect 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 extraseconds! ....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 thefollowing 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 wehave 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 positiveintegers 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, inseconds. 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 largerthan t.

Output

For each test case, print the maximum number of songs (including Jin Ge Jin Qu), and the total lengthsof 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 Qufor another 678 seconds.In the second example, we sing the first two (30+69=99 seconds). Then we still have one secondleft, so we can sing Jin Ge Jin Qu for extra 678 seconds. However, if we sing the first and third songinstead (30+70=100 seconds), the time is already up (since we only have 100 seconds in total), so wecan’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

题目大意:去KTV里唱歌,在快要结束前点一首时间超长的劲歌金曲是非常划算的。假设每首歌只能唱一次且每首歌都可以唱完,歌曲切换的时间不计,现在给出歌曲数量和每首歌的时间,求能够唱歌的最长时间和歌曲数量(劲歌金曲678秒)。

解题思路:题目中给出的总时间t <= 10 ^ 9,然而每首歌时间不超过180s,所以dp数组开到歌曲数量 * 180 + 678即可。这是一个01背包问题,利用一维数组可以优化空间复杂度,还有一个点要注意,由于要记录唱歌的最长时间,所以在dp时只要更新刚好唱完一首歌的时间。如例一,只需更新dp[60],dp[70],dp[80]即可。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

typedef long long ll;

const int maxn = 55;

int song[maxn];
int dp[180 * maxn + 678];

int main()
{
    int T,t,n;
    scanf("%d",&T);
    int ncase = 0;
    while(T--){
        scanf("%d %d",&n,&t);
        for(int i = 1;i <= n;i++){
            scanf("%d",&song[i]);
        }
        int ans = 0;
        memset(dp,-1,sizeof(dp));
        dp[0] = 0;
        for(int i = 1;i <= n;i++){
            for(int j = t - 1;j >= song[i];j--){
                if(dp[j - song[i]] >= 0){
                    dp[j] = max(dp[j],dp[j - song[i]] + 1);
                    ans = max(ans,dp[j]);
                }
            }
        }
        int time;
        for(time = t - 1;time >= 0;time--){
            if(dp[time] == ans){
                break;
            }
        }
        printf("Case %d: %d %d\n",++ncase,dp[time] + 1,time + 678);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值