题目:题目链接

思路:由于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背包的更多相关文章

  1. UVa 12563 (01背包) Jin Ge Jin Qu hao

    如此水的01背包,居然让我WA了七次. 开始理解错题意了,弄反了主次关系.总曲目最多是大前提,其次才是歌曲总时间最长. 题意: 在KTV房间里还剩t秒的时间,可以从n首喜爱的歌里面选出若干首(每首歌只 ...

  2. 紫书 例题 9-5 UVa 12563 ( 01背包变形)

    总的来说就是价值为1,时间因物品而变,同时注意要刚好取到的01背包 (1)时间方面.按照题意,每首歌的时间最多为t + w - 1,这里要注意. 同时记得最后要加入时间为678的一首歌曲 (2)这里因 ...

  3. 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 ...

  4. 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 ...

  5. uVa 12563 Jin Ge Jin Qu

    分析可知,虽然t<109,但是总曲目时间大于t,实际上t不会超过180*n+678.此问题涉及到两个目标信息,首先要求曲目数量最多,在此基础上要求所唱的时间尽量长.可以定义 状态dp[i][j] ...

  6. 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 ...

  7. 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 ...

  8. 一道令人抓狂的零一背包变式 -- UVA 12563 Jin Ge Jin Qu hao

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  9. 【紫书】(UVa12563)Jin Ge Jin Qu hao

    继续战dp.不提. 题意分析 这题说白了就是一条01背包问题,因为对于给定的秒数你只要-1s(emmmmm)然后就能当01背包做了——那1s送给劲歌金曲(?).比较好玩的是这里面dp状态的保存——因为 ...

随机推荐

  1. Java规则引擎drools:drt动态生成规则并附上具体项目逻辑

    一 整合 由于本人的码云太多太乱了,于是决定一个一个的整合到一个springboot项目里面. 附上自己的项目地址https://github.com/247292980/spring-boot 以整 ...

  2. win8.1x64下完美运行IE6

    IE6我相信是所有前端人员永远都绕不过去的坎,操作的版本越来越高,离xp越来越远,难道你还在win8下安装个虚拟机来运行IE6吗?这样即消耗系统资源,来回的切换也麻烦,关键是只为了一个测试哦,没必要这 ...

  3. Xcode Ghost

    Xcode Ghost,是一种手机病毒,主要通过非官方下载的 Xcode 传播,能够在开发过程中通过 CoreService 库文件进行感染,使编译出的 App 被注入第三方的代码,向指定网站上传用户 ...

  4. 7天学完Java基础之2/7

    面向对象 面向对象的思想就是值我们要实现一个共功能的时候,我们不自己去做,而是找别人帮我们去做,帮我们去做的这个人就是对象.面向对象强调的是谁来帮我实现这个功能. 类与对象的关系 类:是一组相关属性和 ...

  5. JAVA4大线程池

    不知不觉中我们电脑的硬件设施越来越好,从双核四线程普及到如今四核八线比比皆是.互联网发展至今,讲究的就是快,less is more,而且大数据的诞生和各种种类繁多的需求处理,单线程的程序逐渐不能满足 ...

  6. leetcode85 Maximal Rectangle

    思路: 分别按行拆分后将这一行之前的每列叠加起来,然后使用leetcode84https://www.cnblogs.com/wangyiming/p/9620942.html的思路计算. 实现: # ...

  7. Catch the moments of your life. Catch them while you're young and quick.

    Catch the moments of your life. Catch them while you're young and quick.趁你还年轻利落,把握住生活中的美好瞬间吧!

  8. zblog添加水印插件后出现Cannot use $this as parameter

    安装了水印插件后后台也进不去了,页面错误提示:Cannot use $this as parameter 删除水印插件文件后恢复正常,具体原因待研究 水印插件文件:/zb_users/plugin/W ...

  9. Spring MVC的高级配置

    1.文件上传配置 文件上传是项目中常用的一个功能,Spring MVC通过配置一个MultipartResolver来上传文件. 在Spring的控制器中,通过MultipartFile file 来 ...

  10. Linux 文件的压缩与解压

    1.  tar结尾压缩命令 [root@test ~]# tar -cvf grub.tar /boot/grub/ 查看压缩包文件 [root@test ~]# tar -vtf grub.tar ...