【动态规划】[UVA12563]Jin Ge Jin Qu hao 劲歌金曲

版权声明:本文为博主原创文章,转载请注明本页地址。 https://blog.csdn.net/C20180630/article/details/75305133

Description

(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) — I know that there are Jin Ge Jin Qu II and III, and some other unofficial 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 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 — 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

题目大意

如果问一个麦霸:“你在KTV里必唱的曲目有哪些?”得到的答案通常都会包含一首“神曲”:古巨基的《劲歌金曲》。为什么呢?一般来说,KTV不会在“时间到”的时候鲁莽地把正在唱的歌切掉,而是会等它放完。例如,在还有15秒时再唱一首2分钟的歌,则实际上多唱了105秒。但是融合了37首歌曲的《劲歌金曲》长达11分18秒1,如果唱这首,相当于多唱了663秒!
假定你正在唱KTV,还剩t秒时间。你决定接下来只唱你最爱的n首歌(不含《劲歌金曲》)中的一些,在时间结束之前再唱一个《劲歌金曲》,使得唱的总曲目尽量多(包含《劲歌金曲》),在此前提下尽量晚的离开KTV。
输入n(n≤50),t( t109 )和每首歌的长度(保证不超过3分钟2),输出唱的总曲目以及时间总长度。输入保证所有n+1首曲子的总长度严格大于t。

分析

虽说 t109 ,但由于所有n+1首曲子的总长度严格大于t,实际上t不会超过180n+678。我们要考虑一些细节,首先最明显的就是所有歌加起来的时间最多为 maxt1 ,因为至少要留一秒唱《劲歌金曲》。还要考虑 maxt=0 的情况,所以就可以得出可以最多唱 max{maxt1,ti} 秒钟。重载运算符(考虑唱的歌最多,其次时间最长)求最优解。这样就可以转化为0-1背包问题了。最后输出最多能唱的歌+1,最多能唱的时间+678。

源代码

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int T,n,maxt,t[51];
struct node{
    int num,time;
    node(){}
    node(int a,int b){num=a;time=b;}//定义结构体构造函数
    bool operator < (const node x) const{//重载运算符,比较最优解
        return (num<x.num)||(num==x.num&&time<x.time);
    }
}dp[9700];//最多不超过180n+678
int main()
{
    scanf("%d",&T);
    for(int num=1;num<=T;num++){
        scanf("%d%d",&n,&maxt);
        int sum=0;
        memset(dp,0,sizeof dp);
        for(int i=1;i<=n;i++){
            scanf("%d",&t[i]);
            sum+=t[i];//求时间总和
        }
        maxt=min(maxt-1,sum);//求最长时间
        for(int i=1;i<=n;i++)//DP
            for(int j=maxt;j>=t[i];j--){
                node tmp(dp[j-t[i]].num+1,dp[j-t[i]].time+t[i]);
                if(dp[j]<tmp) dp[j]=tmp;//比较
            }
        printf("Case %d: %d %d\n",num,dp[maxt].num+1,dp[maxt].time+678);//输出
    }
}

  1. 还有《劲歌金曲2》和《劲歌金曲3》,但本题不予考虑。
  2. 显然大多数歌的长度都大于3分钟,但是KTV可以“切歌”,因此这里的“长度”实际上是指“想唱的时间长度”。

猜你喜欢

转载自blog.csdn.net/C20180630/article/details/75305133