Uva--12563(动规,01背包)

2014-08-26 15:43:40

Problem J

Jin Ge Jin Qu [h]ao

(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

思路:第二版小白书例题。虽然t <= 10^9,但是出了劲歌金曲(678秒)外,其他歌曲最多都只有3分钟(180秒),所以t 不会超过180t + 678,这样就是裸的01背包了,注意初始化细节,由于dp[i]表示歌曲总长等于 i 的最多歌曲数,所以dp初始化应为 特殊值 或者 一个很大的值。

 1 /*************************************************************************
 2     > File Name: 12563.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Tue 26 Aug 2014 02:20:17 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 const int maxn = 10000;
16 const int INF = 1 << 30;
17 
18 int T;
19 int n,t;
20 int dp[maxn];
21 int len[55];
22 
23 int main(){
24     scanf("%d",&T);
25     for(int Case = 1; Case <= T; ++Case){
26         scanf("%d%d",&n,&t);
27         for(int i = 0; i < n; ++i)
28             scanf("%d",&len[i]);
29         len[n] = 678;
30         memset(dp,-1,sizeof(dp));
31         dp[0] = 0;
32         int tmax = -1;
33         for(int i = 0; i <= n; ++i){
34             for(int j = t + len[i] - 1; j >= len[i]; --j){
35                 if(dp[j - len[i]] != -1)
36                     dp[j] = max(dp[j],dp[j - len[i]] + 1);
37                 tmax = dp[j] > tmax ? dp[j] : tmax;
38             }
39         }
40         for(int i = maxn - 1; ; --i)
41             if(dp[i] == tmax){
42                 printf("Case %d: %d %d\n",Case,tmax,i);
43                 break;
44             }
45     }
46     return 0;
47 }
48             

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3937425.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值