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. U盘安装Ubuntu 16.04出现:Failed to load ldlinux.c32

    启动的时候如果不开启UEFI,则会提示: Failed to load ldlinux.c32 Boot failed: please change disks and press a key to ...

  2. Qcon

    http://2017.qconbeijing.com/?utm_source=tuicool&utm_medium=referral

  3. 【Echarts】百度Echarts的使用入门+两个简单的小例子+心得

    Echarts对于展示结果,有一个很好的表达方式. 1.首先,在官网将js下载到本地,引用到页面上 这里是在开发环境,所以下载最后源代码这个 managerResult.jsp <%@ page ...

  4. Android2017最新面试题(3-5年经验个人面试经历)

    2017最新Android面试题 大家好,在跟大家讲述自己的面试经历,以及遇到的面试题前,先说说几句题外话. 接触Android已经3年,在工作中遇到疑难问题总是在网上(csdn大牛博客,stacko ...

  5. Mac的文件比对工具:Meld、Beyond Compare

    Meld开源免费 Beyond Compare可以在Windows.Mac下使用,但是收费,需要自己破解

  6. centos selinux学习记录

    SELinux 全称 Security Enhanced Linux (安全强化 Linux),是 MAC (Mandatory Access Control,强制访问控制系统)的一个实现,目的在于明 ...

  7. http://jingyan.baidu.com/article/dca1fa6fa07000f1a44052f6.html

    http://jingyan.baidu.com/article/dca1fa6fa07000f1a44052f6.html

  8. springboot集成mybatis-generator

    首先上下成功后的效果: 配置非常简单,我们是通过maven插件来进行的,一共只需要3步: 第一步添加mysql依赖和mysql的maven插件: 由于是非常简单的spring+mysql的原始项目,我 ...

  9. [TypeScript] Custom data structures in TypeScript with iterators

    We usually think of types as something that can define a single layer of an object: with an interfac ...

  10. HTML5 Canvas 绘制库存变化折线

    <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type&quo ...