UVa 12563 劲歌金曲 刘汝佳第二版例题9-5;
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;的更多相关文章
- UVA 12563 劲歌金曲(01背包)
劲歌金曲 [题目链接]劲歌金曲 [题目类型]01背包 &题解: 题意:求在给定时间内,最多能唱多少歌曲,在最多歌曲的情况下,使唱的时间最长. 该题类似于01背包问题,可用01背包问题的解题思路 ...
- UVa 12563 劲歌金曲(0-1背包)
https://vjudge.net/problem/UVA-12563 题意: 在一定的时间内连续唱歌,最后一首唱11分钟18秒的劲歌金曲,问最多能长多长时间. 思路: 0-1背包问题,背包容量为t ...
- 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 1(String)
第一题:401 - Palindromes UVA : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8 ...
- c++20701除法(刘汝佳1、2册第七章,暴搜解决)
20701除法 难度级别: B: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 输入正整数n,按从小到大的顺序输出所有 ...
- ACM题目推荐(刘汝佳书上出现的一些题目)[非原创]
原地址:http://blog.csdn.net/hncqp/article/details/1758337 推荐一些题目,希望对参与ICPC竞赛的同学有所帮助. POJ上一些题目在http://16 ...
- 刘汝佳黑书 pku等oj题目
原文地址:刘汝佳黑书 pku等oj题目[转]作者:小博博Mr 一.动态规划参考资料:刘汝佳<算法艺术与信息学竞赛><算法导论> 推荐题目:http://acm.pku.edu. ...
- [置顶] 刘汝佳《训练指南》动态规划::Beginner (25题)解题报告汇总
本文出自 http://blog.csdn.net/shuangde800 刘汝佳<算法竞赛入门经典-训练指南>的动态规划部分的习题Beginner 打开 这个专题一共有25题,刷完 ...
- 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 3(Sorting/Searching)
第一题:340 - Master-Mind Hints UVA:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Item ...
- 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 2(Big Number)
这里的高精度都是要去掉前导0的, 第一题:424 - Integer Inquiry UVA:http://uva.onlinejudge.org/index.php?option=com_onlin ...
随机推荐
- acitivity 和fragment 通信,使用广播来传递信息的问题
使用广播来传递信息时 如果 acitivity 给 太快给 fragment 发送广播,fragment 收不到 使用回调的方式来解决
- faststone 注册码
用户名:c1ikm密码:AXMQX-RMMMJ-DBHHF-WIHTV 或 AXOQS-RRMGS-ODAQO-APHUU
- MySql优化--使用索引优化
原文:http://blog.csdn.net/zuoanyinxiang/article/details/50606837 1.索引优化的原理 在没有使用索引的时候,数据库系统会根据要查找的值到 ...
- Protostuff序列化工具类
源代码 package org.wit.ff.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStre ...
- The method Inflate() in android
Inflate() method can find out a layout defined by xml,as like the findViewById() method,but there ha ...
- centos搭建git服务器(转)
一:git服务安装 1.安装git相关组件 [root@gitserver ~] yum -y install git 2.创建git用户 [root@gitserver ~] groupadd gi ...
- javascript快速入门14--DOM基础
DOM(Document Object Model)——文档对象模型 什么是DOM? Document Object Model (DOM)是HTML和XML文档的编程接口.它提供了上述文档的一种结构 ...
- Java开发中的23种设计模式详解 【转】
创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 行为型模式,共十一种:策略模式.模板方法模式.观察者模式.迭代子模式.责任链模式.命令模式.备忘录模式.状态模式.访问 ...
- gflops
这个网站最棒了 http://kyokojap.myweb.hinet.net/gpu_gflops/
- http://www.cnblogs.com/monian/p/3822980.html
http://www.cnblogs.com/monian/p/3822980.html