Jin Ge Jin Qu hao UVA - 12563 01背包
题目:题目链接
思路:由于t最大值其实只有180 * 50 + 678,可以直接当成01背包来做,需要考虑的量有两个,时间和歌曲数,其中歌曲优先级大于时间,于是我们将歌曲数作为背包收益,用时间作为背包容量进行dp,记录下最多歌曲数目,最后通过最多歌曲数目得出最多歌曲数目下的最长时间,利用滚动数组我们只需要开一维数组即可
AC代码:
import java.util.Arrays;
import java.util.Scanner; public class Main { final public static int maxn = 10000; public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T, n, len; int[] f = new int[maxn];
int[] t = new int[55];
T = in.nextInt();
for(int kase = 1; kase <= T; ++kase) {
Arrays.fill(f, 0); n = in.nextInt();
len = in.nextInt();
for(int i = 0; i < n; ++i)
t[i] = in.nextInt();
int _max = 0;
for(int i = 0; i < n; ++i) {
for(int j = len - 1; j >= t[i]; --j) {
if(f[j - t[i]] >= 1 || j == t[i]) {
f[j] = Math.max(f[j], f[j - t[i]] + 1);
_max = Math.max(_max, f[j]);
}
}
}
int i;
for(i = len - 1; f[i] != _max; --i)
;
if(_max == 0)
System.out.format("Case %d: %d %d\n", kase, 1, 678);
else
System.out.format("Case %d: %d %d\n", kase, _max + 1, i + 678);
}
in.close();
}
}
Jin Ge Jin Qu hao UVA - 12563 01背包的更多相关文章
- UVa 12563 (01背包) Jin Ge Jin Qu hao
如此水的01背包,居然让我WA了七次. 开始理解错题意了,弄反了主次关系.总曲目最多是大前提,其次才是歌曲总时间最长. 题意: 在KTV房间里还剩t秒的时间,可以从n首喜爱的歌里面选出若干首(每首歌只 ...
- 紫书 例题 9-5 UVa 12563 ( 01背包变形)
总的来说就是价值为1,时间因物品而变,同时注意要刚好取到的01背包 (1)时间方面.按照题意,每首歌的时间最多为t + w - 1,这里要注意. 同时记得最后要加入时间为678的一首歌曲 (2)这里因 ...
- UVA - 12563 Jin Ge Jin Qu hao (01背包)
InputThe first line contains the number of test cases T (T ≤ 100). Each test case begins with two po ...
- 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 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状态的保存——因为 ...
随机推荐
- 让我们把KBEngine玩坏吧!如何定制我们自己的C++函数(一)
为什么不更新kbe warring的代码解读了,因为在我看来那个demo讲完了实体就没东西可讲了,如果专心的看官方文档和PPT的话demo的代码后面没任何难点了已经,单纯的复制黏贴代码实在太过无聊.程 ...
- Android入门:封装一个HTTP请求的辅助类
前面的文章中,我们曾经实现了一个HTTP的GET 和 POST 请求: 此处我封装了一个HTTP的get和post的辅助类,能够更好的使用: 类名:HttpRequestUtil 提供了如下功能: ( ...
- Vue-watch选项
Vue ----watch 选项 用于 监听数据变化: <!DOCTYPE html> <html lang="en"> <head> < ...
- Myeclipse 突然打不开的问题
用的好好的Myeclipse今天突然打不开了,打开myeclipse提示 :an error has occurred see the log file 然后我打开日志文件,看到如下的报错信息: ! ...
- c语言数组赋值
int acct_parm[3] = {4, 2, 30};#define RESUME (acct_parm[0])
- 阿里 EasyExcel 7 行代码优雅地实现 Excel 文件生成&下载功能
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...
- VMware下Centos6.4安装
VMware(Virtual Machine ware)是一个“虚拟PC”软件公司,提供服务器.桌面虚拟化的解决方案. 小伙伴们网上下载VMware11,一路下一步自己安装吧!!! 打开 VMware ...
- 怎么旋转PDF文件的方向并保存成功
http://jingyan.baidu.com/article/59a015e39d7802f79488651e.html PDF格式的文档是非常普遍的一种阅读电子书格式,基本上非常好用了,不过有时 ...
- centos开机启动自定义脚本
有些时候我们需要在服务器里设置一个脚本,让他一开机就自己启动.方法如下: cd /etc/init.d vi youshell.sh #将youshell.sh修改为你自己的脚本名 编写自己的脚本后保 ...
- cms-帖子静态化
实现帖子静态化和实现友情链接静态化一致, 1.首先建立帖子类别的实体类: package com.open1111.entity; /** * 帖子类别实体 * @author user * */pu ...