UVA - 12563 Jin Ge Jin Qu hao (01背包)
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
#include <iostream>
#include <string.h>
using namespace std;
const int MAX = * * + ;
struct Node {
int n, t; bool operator<(const Node &rhs) const {
return n < rhs.n ||
n == rhs.n && t < rhs.t;
}
}d[MAX]; int main() {
int n,cas,t,ti;
cin >> cas;
for (int cass = ; cass <= cas; ++cass) {
cin >> n >> t;
memset(d, , sizeof(d));
for (int i = ; i <= n; ++i) {
cin >> ti;
for (int v = t; v > ti; --v) { //0,1背包,滚动数组
Node tmp;
tmp.n = d[v - ti].n + ;
tmp.t = d[v - ti].t + ti;
if (d[v] < tmp)
d[v] = tmp;
}
}
printf("Case %d: %d %d\n", cass, d[t].n + , d[t].t + ); //最后金曲也要算进去
}
}
UVA - 12563 Jin Ge Jin Qu hao (01背包)的更多相关文章
- UVA12563Jin Ge Jin Qu hao(01背包)
紫书P274 题意:输入N首歌曲和最后剩余的时间t,问在保证能唱的歌曲数目最多的情况下,时间最长:最后必唱<劲歌金曲> 所以就在最后一秒唱劲歌金曲就ok了,背包容量是t-1,来装前面的歌曲 ...
- 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 ...
- uVa 12563 Jin Ge Jin Qu
分析可知,虽然t<109,但是总曲目时间大于t,实际上t不会超过180*n+678.此问题涉及到两个目标信息,首先要求曲目数量最多,在此基础上要求所唱的时间尽量长.可以定义 状态dp[i][j] ...
- 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, se ...
- 12563 Jin Ge Jin Qu hao
• Don’t sing a song more than once (including Jin Ge Jin Qu). • For each song of length t, either si ...
- UVa 12563 (01背包) Jin Ge Jin Qu hao
如此水的01背包,居然让我WA了七次. 开始理解错题意了,弄反了主次关系.总曲目最多是大前提,其次才是歌曲总时间最长. 题意: 在KTV房间里还剩t秒的时间,可以从n首喜爱的歌里面选出若干首(每首歌只 ...
- Jin Ge Jin Qu hao UVA - 12563 01背包
题目:题目链接 思路:由于t最大值其实只有180 * 50 + 678,可以直接当成01背包来做,需要考虑的量有两个,时间和歌曲数,其中歌曲优先级大于时间,于是我们将歌曲数作为背包收益,用时间作为背包 ...
- 一道令人抓狂的零一背包变式 -- UVA 12563 Jin Ge Jin Qu hao
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
- 【紫书】(UVa12563)Jin Ge Jin Qu hao
继续战dp.不提. 题意分析 这题说白了就是一条01背包问题,因为对于给定的秒数你只要-1s(emmmmm)然后就能当01背包做了——那1s送给劲歌金曲(?).比较好玩的是这里面dp状态的保存——因为 ...
随机推荐
- 用cookie登录慕课网络教学中心刷评论
声明:本文仅供学习参考 我们学校和的网络教学平台是在慕课网上的,需要登录到慕课网的教学平台以后,拿到cookie 注意:没次提交后需要休眠,否则刷评论过快会被系统发现 如果请求太快,很容易被系统发现( ...
- uoj problem 31 猪猪侠再战括号序列
题目大意: 给定一个长度为2n的括号序列.定义一个关于区间[l,r]的翻转操作为位置平移对调. 即翻转")))()("可以得到"()()))((" 用不超过n次 ...
- 如何恢复,迁移,添加, 删除 Voting Disks
如何恢复,迁移,添加, 删除 Voting Disks恢复流程 在11gR2 之前,我们可以直接直接使用dd命令对voting disk进行备份.DD示例 备份votedisk盘:[root@raw1 ...
- html之canvas
canvas代码片段: <canvas id="testCanvas" width="400" height="150" style= ...
- 洛谷 2312 / bzoj 3751 解方程——取模
题目:https://www.luogu.org/problemnew/show/P2312 https://www.lydsy.com/JudgeOnline/problem.php?id=3751 ...
- NB-IoT知识
通常,我们把物联网设备分为三类: ①无需移动性,大数据量(上行),需较宽频段,比如城市监控摄像头. ②移动性强,需执行频繁切换,小数据量,比如车队追踪管理. ③无需移动性,小数据量,对时延不敏感,比如 ...
- [快手(AAuto)学习笔记]如何让程序在运行时请求管理员权限(UAC)
作者:ffsystem 作为(糟糕的)程序猿,习惯写代码解决一些简单事务.正常用批处理就能解决大部分工作,复杂一点用AutoIt 3. 有时候要分发给别人,就需要一个界面.外行你程序写得如何他看不懂, ...
- 接口Comparator和Comparable的区别和联系
1. Comparator 和 Comparable 相同的地方 他们都是java的一个接口, 并且是用来对自定义的class比较大小的. 什么是自定义class: 如 public class Pe ...
- 2 ubuntu 16.04 安装Elastic Stack
一: 安装JAVA8 添加ppa sudo add-apt-repository ppa:webupd8team/java sudo apt-get update 安装oracle- ...
- 51单片机的TXD、 RXD 既接了 232 又接了 485芯片 ,会导致通信失败!
51单片机的TXD. RXD 既接了 232 又接了 485 ,会导致通信失败! 下面是绘制电路板用的部分电路图: 通信现象: 1.我使用了USB-232的下载模块,把它接到P4上,发现单片机只能发送 ...