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

题意:

1,在时间t内唱的歌数量越多越好;
2,还要求唱歌总时间越长越好;
至少留出一秒钟来唱jin ge jin qu;因为必须选择唱jin ge jin qu 才能最优;
所以用t-1时间来选择唱给出的n首歌;

尽量唱的数量多,在数量相同时尽量时间长;

思路:把时间当做花费,数量当做价值,进行01背包,因为由于递推的原因会导致无法确认数量最大时,时间是多少;所以要初始化dp为一个特殊值来判断;即完全背包;
这样就可以保证数量最大时刻的时间处为耗费的总时间;

链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34887

#include<iostream>

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<string>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
int dp[10000];
const int inf = 0x3f3f3f3f;
int next[55];
int a[55], t, n;
int main()
{
    int T, cas=1;
    cin>>T;
    while(T--)
    {
        scanf("%d%d",&n,&t);
        for(int i = 1; i <= n; i++){
            scanf("%d",&a[i]);
        }
        for(int i = 1; i < 10000; i++) dp[i] = -inf;
        dp[0] = 0;
        for(int i = 1; i <= n; i++){
            for(int j = t-1; j >= a[i]; j--){
                dp[j] = max(dp[j],dp[j-a[i]]+1);
            }
        }
        int time ,cnt, mas = -inf, pos;
        for(int i = t-1; i >= 0; i--){
            if(dp[i]>mas){
                mas = dp[i]; pos = i;
            }
        }
 
        printf("Case %d: %d %d\n",cas++,mas+1,pos+678);//最后一秒用来唱jin ge jin qu;
    }
    return 0;
}

UVa 12563 劲歌金曲 刘汝佳第二版例题9-5;的更多相关文章

  1. UVA 12563 劲歌金曲(01背包)

    劲歌金曲 [题目链接]劲歌金曲 [题目类型]01背包 &题解: 题意:求在给定时间内,最多能唱多少歌曲,在最多歌曲的情况下,使唱的时间最长. 该题类似于01背包问题,可用01背包问题的解题思路 ...

  2. UVa 12563 劲歌金曲(0-1背包)

    https://vjudge.net/problem/UVA-12563 题意: 在一定的时间内连续唱歌,最后一首唱11分钟18秒的劲歌金曲,问最多能长多长时间. 思路: 0-1背包问题,背包容量为t ...

  3. 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 1(String)

    第一题:401 - Palindromes UVA : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8 ...

  4. c++20701除法(刘汝佳1、2册第七章,暴搜解决)

    20701除法 难度级别: B: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述     输入正整数n,按从小到大的顺序输出所有 ...

  5. ACM题目推荐(刘汝佳书上出现的一些题目)[非原创]

    原地址:http://blog.csdn.net/hncqp/article/details/1758337 推荐一些题目,希望对参与ICPC竞赛的同学有所帮助. POJ上一些题目在http://16 ...

  6. 刘汝佳黑书 pku等oj题目

    原文地址:刘汝佳黑书 pku等oj题目[转]作者:小博博Mr 一.动态规划参考资料:刘汝佳<算法艺术与信息学竞赛><算法导论> 推荐题目:http://acm.pku.edu. ...

  7. [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总

    本文出自   http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner  打开 这个专题一共有25题,刷完 ...

  8. 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 3(Sorting/Searching)

    第一题:340 - Master-Mind Hints UVA:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Item ...

  9. 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 2(Big Number)

    这里的高精度都是要去掉前导0的, 第一题:424 - Integer Inquiry UVA:http://uva.onlinejudge.org/index.php?option=com_onlin ...

随机推荐

  1. Android 架构 4.总结

    以下是Keegan小钢大神原创博客: Android项目重构之路:架构篇Android项目重构之路:界面篇Android项目重构之路:实现篇 看了这几篇文章,以及下面的评论,总结一下,以便以后拓展: ...

  2. HDU 4606 Occupy Cities (计算几何+最短路+二分+最小路径覆盖)

    Occupy Cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. Ubuntu安装“启动引导器”的设备选哪一项,选默认还是选/boot分区?

    如果你要用Ubuntu的引导器代替Windows的引导器,就选 /dev/sda. 如果你要保留Windows的引导器,就选 /boot分区,但这样一来,装完Ubuntu重启后,只能启动Windows ...

  4. UdpClient类客户端和服务端demo

    服务端demo static IPEndPoint ipe = new IPEndPoint(IPAddress.Any, 0); static UdpClient udp = new UdpClie ...

  5. SpringBoot下文件上传与下载的实现

    原文:http://blog.csdn.net/colton_null/article/details/76696674 SpringBoot后台如何实现文件上传下载? 最近做的一个项目涉及到文件上传 ...

  6. shell产生随机数七种方法

    shell实例浅谈之三产生随机数七种方法   一.问题 Shell下有时需要使用随机数,在此总结产生随机数的方法.计算机产生的的只是“伪随机数”,不会产生绝对的随机数(是一种理想随机数).伪随机数在大 ...

  7. 配置composer全量镜像与主要命令

    配置中国全量镜像 查看当前composer配置的镜像地址 composer config -g repo.packagist 显示如下,显示说明没有配置镜像地址 接下来我使用下面的命令进行查看配置的镜 ...

  8. Node.js meitulu图片批量下载爬虫1.02版

    以前版本需要先查看网页源码,然后肉眼找到图片数量和子目录,虽说不费事,但多少有点不方便. 于是修改了一下,用cheerio自己去找找到图片数量和子目录,只要修改页面地址就行了.至此社会又前进了一步. ...

  9. css - border-radius

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. javascript - 你不容错过的es6模板写法

    /** * ``即重音符(128键盘左上角ESC下面那个键盘) * 隶属:模板字符串 */ let unit = '4'; let keywords = 'uc'; // step1:模板变量 ${v ...