UVA - 12563 Jin Ge Jin Qu hao (01背包变形)
此题应该注意两个点,首先背包容量应该缩减为t-1,因为最长的歌不超过三分钟,而劲歌金曲有678s,所以肯定要留出这个时间来。其次注意优先级,保证唱的歌曲数目最多,在此前提下尽可能的延长时间。
处理方法:开创结构体,在歌曲数目相等的时候,选取最长时间。
注意:注意t的大小,t完全没有必要计算那么大的数据。 代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 10010
int w[];
struct DP
{
int song,tim;
}dp[N];
int main()
{
// freopen("1.in.cpp","r",stdin);
int T,n,t,ca=,sum;
cin>>T;
while(T--)
{
cin>>n>>t;
sum = ;
for(int i = ; i <= n; i++) {
cin>>w[i];
sum += w[i];
}
if(t > sum){
if(t < sum + ) t = sum + ;
printf("Case %d: %d %d\n",++ca,n+,t);
continue;
}
t--;
for(int i = ;i < N;i++) dp[i].song = dp[i].tim = ;
for(int i = ; i <= n; i++)
{
for(int j = t; j >= w[i]; j--)
{
if(dp[j-w[i]].song+ > dp[j].song)
{
dp[j].song = dp[j-w[i]].song + ;
dp[j].tim = dp[j-w[i]].tim + w[i];
}
if(dp[j-w[i]].song+ == dp[j].song)
{
dp[j].tim = max(dp[j].tim,dp[j-w[i]].tim + w[i]);
}
}
}
printf("Case %d: %d %d\n",++ca,dp[t].song+,dp[t].tim+);
}
return ;
}
UVA - 12563 Jin Ge Jin Qu hao (01背包变形)的更多相关文章
- 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 ...
- 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 ...
- FZU 2214 Knapsack problem 01背包变形
题目链接:Knapsack problem 大意:给出T组测试数据,每组给出n个物品和最大容量w.然后依次给出n个物品的价值和体积. 问,最多能盛的物品价值和是多少? 思路:01背包变形,因为w太大, ...
- codeforce Gym 101102A Coins (01背包变形)
01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...
- HDU 2639 Bone Collector II(01背包变形【第K大最优解】)
Bone Collector II Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 【01背包变形】Robberies HDU 2955
http://acm.hdu.edu.cn/showproblem.php?pid=2955 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...
随机推荐
- php 排序
for($i=1;$i<count($result);$i++) { $tmp = $result[$i]['PNL']; $tmp_ = $result[$i]; $j=$i-1 ; for( ...
- winform实现矩形框截图
使用方法如下: private void button1_Click(object sender, EventArgs e) { s.GerScreenFormRectangle(); } priva ...
- redis win版安装
直接来看看redis怎么安装到windows系统上,并开启他的服务. 可以在这里下载https://github.com/dmajkic/redis/downloads. 我用的是64位的,解压后的结 ...
- Objective-C中的instancetype与id的区别
一.什么是instancetype instancetype是clang 3.5开始,clang提供的一个关键字,表示某个方法返回的未知类型的Objective-C对象.我们都知道未知类型的的对象可以 ...
- ios 中Category类别(扩展类)小结
类别 类别是一种为现有的类添加新方法的方式.利用Objective-C的动态运行时(runtime)分配机制,可以为现有的类添加新方法,这种为现有的类添加新方法的方式称为类别catagory,他可以为 ...
- Program Files 与Program Files (x86)
在64位系统下,为了更好的兼容32位程序,在安装一些32位程序(注意某些程序他就是32位的),会默认扔到program files(x86)这个文件夹下,而一些64位的程序,或程序本身没有位数区别的, ...
- grub引导centos
下面来主要讲一下在grub下来引导centos: 其步骤如下; a 进入grub的命令模式. b 先熟悉一下grub 的一些命令 grub>help c 熟悉一下cat命令 d ro ...
- 安装cocoapods遇到的一些问题
其实可以直接到https://github.com/CocoaPods/Specs上把所需的文件夹下载下来 解压后命名为master放在本地的 ~/.cocoapods/repos 下面就行 但是使 ...
- Android:关于服务的总结
服务 startService(intent) stopService(intent) 服务的生命周期 如果采用start的方式开启服务 oncreate()--->onstartcommand ...
- List循环与Map循环的总结
做了一下list和map的总结,没有什么技术含量,就全当复习了一下api. 测试环境是在junit4下,如果没有自己写一个main方法也是一样的. 首先是List的三种循环: @Test public ...