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状态的保存——因为 ...
随机推荐
- [技术交流ppt]babel7中preset-env的优化使用
备注 pdf在这里
- 【练习笔记】spring 配置Schedule
spring项目一些简单的定时任务可以通过@Schedule注解来实现,具体配置如下 在applicationContext.xml文件中增加配置 1.引入task约束 xmlns:task=&quo ...
- Vue系列:关于侵权前言
因为一开始学习 Vue 的时候,秉持的是"好记性不如烂笔头"的思想,所以这些知识点都是当时查阅然后选择性摘录下来的. 当时并没有很刻意去记录原文出处(现在我知错了..),所以..如 ...
- substring、slice、substr的区别
首先定义一个变量便于下面测试:var str = "xx351223441"; substring: str.substring(form,to):从字符串里截取下标为form ...
- oracle数据库,表被锁,如何解锁?
4.批量解锁declare cursor mycur isselect b.sid,b.serial# from v$locked_object a,v$session bwhere a.sessio ...
- python3基础06(随机数的使用)
#!/usr/bin/env python# -*- coding:utf-8 -*- import osimport randomimport string la=[0,1,2,3,4,5,6,7, ...
- SQLServer从其他表获取的数据更新该表的一部分
在网上常见的是update a set username = username FROM b on a.userid=b.userid,该更新语句是对a表中所有行进行更新.如果只更新一部 ...
- Centos install ICU, INTL for php
1. Install ICU from source wget http://download.icu-project.org/files/icu4c/56.1/icu4c-56_1-src.tgz ...
- IOS 移除栈顶的控制器
- (IBAction)back2Two:(id)sender { // 移除当前栈顶的控制器 [self.navigationController popViewControllerAnimated ...
- 编译防火墙——C++的Pimpl惯用法解析
http://blog.csdn.net/lihao21/article/details/47610309 Pimpl(pointer to implementation, 指向实现的指针)是一种常用 ...