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 [题意] 有一个强盗要去几个银行偷盗,他既想多抢点钱,又想尽量不被抓到.已知各个银行 的金钱数和被抓的概率 ...
随机推荐
- bkViewer V4.9k(数码照片浏览工具)中文免费版
软件名称: bkViewer 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 990KB 图片预览: 软件简介: 专为 ...
- 2015 Multi-University Training Contest 6
1001 Average 忍不住又补了一题. 只要枚举1与2之间1给2,2给1,什么都不做三种状态. 后面的情况都已经决定了. (估计只有我比赛的时候把a candy当成a个糖果了吧QAQ) # in ...
- localStorage请使用getItem 和setITem
最近看别人的代码,发现他们在从localStorage里面的时候喜欢用dot来操作,而不是get setItem,记得以前说过这个事.下面再说一次吧. 用dot方式来操作( 每次以'hello'= ...
- UVALive - 3026 Period kmp next数组的应用
input n 2<=n<=1000000 长度为n的字符串,只含小写字母 output Test case #cas 长度为i时的最小循环串 循环次数(>1) 若没有则不输出 做法 ...
- sql or 与and同时有时要注意
如果是一天sql语句中有or和and同时在 正确:where xxx=xx and (xxx=xx or xxx=xx) 错误:where xxx=xx and xxx=xx or xxx=xx (这 ...
- The first to Python
今天在51cto购买了python教程,今后在这里记录我python的点点滴滴,感谢博客园给予的平台,感谢51cto给予的机会,感谢导师.
- android应用的优化建议(转载)
首先,这是我在http://www.oschina.net/translate/40-developer-tips-for-android-optimization看到的一片文章,感觉挺有道理的,所以 ...
- apk的重签名
1. 生成Android APK包签名证书 1). 在doc中切换到jdk的bin目录 cd C:\Program Files\Java\jdk1.6.0_18\bin 2). ...
- OutputDebugString输出调试信息到debugtrack
OutPutDebugString()函数的输出则可以用DebugView捕获(DebugView也可以捕获TRACE宏的输出)eg: OutPutDebugString("输出第一调试信息 ...
- hdu_1513_Palindrome(LCS+滚动数组)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意:给你一个字符串,问你最少插入多少个字符使其为回文字符. 题解:将字符串倒着保存,然后求一下 ...