题目:题目链接

思路:由于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. IoC和AOP使用扩展。。。

    实现依赖的多种方式. 1.理解构造注入. 2.掌握使用p命名空间实现属性注入. 3.理解不同的数据类型的注入方式. 4.如何通过构造注入为业务类注入所依赖的数据访问层对象,实现保存用户数据功能. 5. ...

  2. base、self标签

    以新的窗口打开页面 self在自己的窗口打开

  3. Eucalyptus-利用镜像启动一个Centos实例

    1.前言 使用kvm制作Eucalyptus镜像(Centos6.5为例)——http://www.cnblogs.com/gis-luq/p/3990795.html 上一篇我们讲述了如何利用kvm ...

  4. 关于HTML中时间格式以及查询数据库的问题

    1.默认时间格式,加入属性dateFormate="yyyy-MM-dd" 2.设置默认值,value="2017-6-22" 3.在JavaScript中将获 ...

  5. leetcode--5 Longest Palindromic Substring

    1. 题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximu ...

  6. MYSQL短索引

    优化MYSQL时,可以尽量使用短索引,如果只是为了提高读取的速度,可以优先使用聚合索引,把几个字段聚集在一起,当然缺点在于操作(写)的时候会降低效率,短索引一般都是开头几个字符基本不同的时候,可以考虑 ...

  7. securetextentry 切换后有空格问题解决

    搜索发现这个问题,网上的解决方法不明确,对于小白怎么办 直接上代码: - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundCo ...

  8. 2018.6.22 Java试题测试结果

    如何从有数字规律的网址抓取网页并保存在当前目录?假设网址为 http://test/0.xml,其中这个数字可以递增到100. for((i=0;i<100;++i));do wget http ...

  9. php xdebug扩展无法进入断点问题

    Waiting for incoming connection with ide key 看到这句话就恶心 这两天搞php运行环境搞的头大,好在现在终于调通了,可以正常进入断点了 现在记录一下,避免下 ...

  10. python剑指offer 合并两个排序的链表

    题目描述 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则. # -*- coding:utf-8 -*- # class ListNode: # def _ ...