UVA Jin Ge Jin Qu hao 12563

Jin Ge Jin Qu hao

(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 someother 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 songbefore 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!!!

  Nowthat 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 alsomaximizes 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里面有n首歌曲你可以选择,每首歌曲的时长都给出了. 对于每首歌曲,你最多只能唱1遍. 现在给你一个时间限制t (t<=10^9) , 问你在最多t-1秒的时间内可以唱多少首歌曲num , 且最长唱歌时间是多少time (time必须<=t-1) ? 最终输出num+1 和 time+678 即可.

注意: 你需要优先让歌曲数目最大的情况下,再去选择总时长最长的.

思路:

  其实本题本质上就是一个标准的01背包问题. 问你<=t-1时间内最多可以选择哪些歌曲使得 (数据1,数据2) 最优。 这里的数据1是歌曲数目,数据2是歌曲总时长,且数据1优先。

一般我们做的01背包问题都是问你<=t-1的时间内, 最多选择哪些歌曲使得歌曲数目最多总时间最长。 但是本题需要同时考虑两个最优条件, 那么该怎么做呢?

我们令dp[i][j]==x 表示当决策完全前i个物品后(选或不选), 所选的总歌曲时长<=j时, 所得到的最优状态为x. (这里的x就不是平时我们所说的最长时间或最多歌曲数目了)

怎么理解最优状态为x这个事实呢? 假设有两种选择前i个歌曲的方法能使得决策完前i个物品且总时长<=j时的状态分别为x1 和x2.

那么如果x1状态的歌曲数目> x2状态的歌曲数目, 那么明显x1状态更优. 所以dp[i][j]应==x1.

如果x1状态的歌曲数目与x2的相等, 但是x2状态的时长 > x1状态时长, 那么此时x2状态更优. 所以dp[i][j]应==x2.

经过上面的分析,我们可以用一个(具有歌曲数和总时长双属性的)结构体来表示一个状态. 且可以得到下面状态转移公式:

dp[i][j] = 最优( dp[i-1][j]  在dp[i-1][j-t[i]]的基础上选择第i首歌后得到的新状态tmp )

所有dp初始化为0即可. 最终我们所求为dp[n][max_time]

最后还有一个问题就是t<=10^9.我们不可能循环判断j到10^9. 其实一共50首歌曲, 每首歌曲最多180秒, 所以我们求出所有歌曲的时长和sum(sum<=50*180==9000).

如果t-1>=sum, 那么明显所有歌曲都能被选一遍.

如果t-1<sum,那么明显我们需要遍历到dp[i][t-1]为止.

程序实现用的滚动数组,所以dp只有[j]这一维.

错因:1.数组开小了。2.输入不能用cin,会RE。

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
int T,n,t,sum,tot,num[60];
struct nond{
    int num,time;
}tmp,f[180*55+678];
nond max(nond a,nond b){
    if(a.num==b.num)
        if(a.time<b.time)    return b;
        else return a;
    if(a.num<b.num)    return b;
    else return a;
}
int main(){
    scanf("%d",&T);
    while(T--){
        sum=0;
        memset(f,0,sizeof(f));
        memset(num,0,sizeof(num));
        scanf("%d%d",&n,&t);
        for(int i=1;i<=n;i++){
            scanf("%d",&num[i]);
            sum+=num[i];
        }
        t=min(t-1,sum);
        for(int i=1;i<=n;i++)
            for(int j=t;j>=num[i];j--){
                tmp.num=f[j-num[i]].num+1;
                tmp.time=f[j-num[i]].time+num[i];
                f[j]=max(f[j],tmp);
            }
        printf("Case %d: ",++tot);
        printf("%d %d\n",f[t].num+1,f[t].time+678);
    }
    return 0;
} 
时间: 2024-10-06 21:26:45

UVA Jin Ge Jin Qu hao 12563的相关文章

uva 12563 - Jin Ge Jin Qu hao(动态规划~劲!歌!金!曲!)

错的我真是无语...还是状态的把握不准确.. 起始状态转移方程是很重要,但是只推出了方程是不够的 对边界状态的处理,对特殊状态的处理,这些都很重要,错了任何一个小地方,都会导致WA.... 细节!更清晰的思路,更全面的考虑! #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,t; int v[55]; int d[55][10000]; int path[5

UVA 12563 Jin Ge Jin Qu hao 01背包变形

基本的01背包,更新的时候保持背包里每一个元素的num最大然后time尽量长 CSDN也支持makedown了试一下 12563 Jin Ge Jin Qu hao (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 popul

12563 - Jin Ge Jin Qu hao——[DP递推]

(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

UVA 12563 Jin Ge Jin Qu hao(01背包变形:两个背包内容)

题意: KTV里面有n首歌曲你可以选择,每首歌曲的时长都给出了. 对于每首歌曲,你最多只能唱1遍. 现在给你一个时间限制t (t<=10^9) , 问你在最多t-1秒的时间内可以唱多少首歌曲num , 且最长唱歌时间是多少time (time必须<=t-1) ? 最终输出num+1 和 time+678 即可. 注意: 你需要优先让歌曲数目最大的情况下,再去选择总时长最长的. //0 KB 39 ms #include<cstdio> #include<cstring>

UVA 12563 Jin Ge Jin Qu hao DP

背包可行性  有点SB,wa了好几发 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <queue> #include <deque> #include <bitset> #

UVA 12563(Jin Ge Jin Qu hao)

开始认真学DP.我对滚动数组的理解是:后一个状态可以由前一个状态求得,便可以使用一维数组重复利用节省空间复杂度. 这个题要注意题目要求的前提,求次数可以看作重量为v[i]价值为1放入w-1的背包,歌曲就是重量和价值都是v[i],简单的转化. #include <iostream> #include <bits/stdc++.h> using namespace std; const int maxn = 100000+10; int dp[maxn]; int v[maxn]; i

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

如此水的01背包,居然让我WA了七次. 开始理解错题意了,弄反了主次关系.总曲目最多是大前提,其次才是歌曲总时间最长. 题意: 在KTV房间里还剩t秒的时间,可以从n首喜爱的歌里面选出若干首(每首歌只能唱一次且如果唱就必须唱完),然后剩下至少1秒的时间来唱那首长678秒的歌曲. 总曲目最多的前提下,尽量使歌曲总时间最长. 分析: 所给时间为t,在t-1秒内进行01背包,num[i]来记录剩余时间为 i 时能长的最多曲目,如果曲目相同还要记录最长时间. 1 //#define LOCAL 2 #i

UVa 12563 Jin Ge Jin Qu hao【01背包】

题意:给出t秒时间,n首歌分别的时间a[i],还给出一首长度为678的必须唱的劲歌金曲,问最多能够唱多少首歌(只要最后时间还剩余一秒,都可以将劲歌金曲唱完) 用dp[i]代表花费i时间时唱的歌的最大数量 背包容量即为给出的总时间t-1(留一秒钟唱劲歌金曲) ,每首歌的代价为a[i], 然后状态转移方程为 dp[j]=max(dp[j],dp[j-a[i]]+1); 自己写的时候,一直一直wa 后来看了lrj的代码,发现是初始化不对, 改掉初始化就对了= = 可是为什么要这样初始化呢= = 后来才

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

此题应该注意两个点,首先背包容量应该缩减为t-1,因为最长的歌不超过三分钟,而劲歌金曲有678s,所以肯定要留出这个时间来.其次注意优先级,保证唱的歌曲数目最多,在此前提下尽可能的延长时间. 处理方法:开创结构体,在歌曲数目相等的时候,选取最长时间. 注意:注意t的大小,t完全没有必要计算那么大的数据. 代码如下: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm>