题目链接:https://uva.onlinejudge.org/external/125/12563.pdf

题意:n首歌,每首歌的长度给出,还剩 t 秒钟,由于KTV不会在一首歌没有唱完的情况下切歌,求在总曲目尽量多的情况下,唱的最久。

分析:

刚开始,题意看错了,结果就按01背包模板了,求了在 t 时间下唱的最久,然后再找出唱了几首歌。WA到疯了,最后实在是崩溃啊!

然后,这个题目没做出来,主要还是01背包没弄透彻。可以利用二维 [2][t] 的滚动数组求路径,这里不仅是优化了空间,而且,当我找到最优值的时候,可以很快找到路径 [p^1][]之前已经被覆盖,也就是说,哪些歌被选了,就有[p^1][t] == ans;

然后要理解p^1,这样的覆盖,刚开始p=1;p^1,进行了初始化,递推,每递推一层,p = p^1;最后,p被转化到下一个状态了,在找路径的时候要转化回来。

/*
#include <bits/stdc++.h>
using namespace std; #define C 1000000 int v[55];
int w[55];
int f[C]; int main()
{ int T;
int cases = 1;
scanf("%d",&T);
while(T--)
{
int n,t;
scanf("%d%d",&n,&t); int temp;
for(int i=1; i<=n; i++)
{
scanf("%d",&temp);
v[i] = temp;
w[i] = temp;
} memset(f,0,sizeof(f)); for(int i=1; i<=n; i++)
{
for(int j=t; j>=0; j--)
{
if(j>=v[i])
f[j] = max(f[j],f[j-v[i]]+w[i]);
}
} //printf("%d\n",f[t]);
//printf("%d\n",f[t-1]); vector<int> ans;
ans.clear();
int sum = f[t];
for(int i=n; i>=1; i--)
{
if(sum-v[i]==f[sum-v[i]])
{
ans.push_back(v[i]);
sum-=v[i];
}
} sort(ans.begin(),ans.end()); if(f[t]>=t)
{
ans.clear();
int the_ans=-1;
for(int i=0; i<t; i++)
{
if(f[i]<f[t])
{
if(f[i]>the_ans)
the_ans = f[i];
}
}
int sum = the_ans;
for(int i=n; i>=1; i--)
{
if(sum-v[i]==f[sum-v[i]])
{
ans.push_back(v[i]);
sum-=v[i];
}
}
printf("Case %d: %d %d\n",cases++,ans.size()+1,the_ans+678);
}
else if(f[t]<t)
printf("Case %d: %d %d\n",cases++,ans.size()+1,f[t]+678); } return 0;
}
*/
#include <bits/stdc++.h>
using namespace std; int n,t; const int maxn=; int len[maxn];
int d[][maxn*+]; int main()
{
int T;
scanf("%d",&T); for(int kase = ; kase<=T; kase++)
{
scanf("%d%d",&n,&t);
for(int i=; i<=n; i++)
scanf("%d",&len[i]); for(int i=; i<t; i++)
d[][i] = -;
d[][] = ; int p = ,ans =;
for(int i=; i<=n; i++)
{
for(int j=; j<t; j++)
{
d[p][j] = d[p^][j];
if(j>=len[i]&&d[p^][j-len[i]]>=)
{
d[p][j] = max(d[p][j],d[p^][j-len[i]]+);
}
ans = max(ans,d[p][j]);
}
p = p^;
}
for(int i=t-; i>=; i--)
{
if(d[p^][i]==ans)
{
printf("Case %d: %d %d\n",kase,ans+,i+);
break;
}
}
} return ;
}

Uva 12563,劲歌金曲,01背包的更多相关文章

  1. UVA 12563 劲歌金曲(01背包)

    劲歌金曲 [题目链接]劲歌金曲 [题目类型]01背包 &题解: 题意:求在给定时间内,最多能唱多少歌曲,在最多歌曲的情况下,使唱的时间最长. 该题类似于01背包问题,可用01背包问题的解题思路 ...

  2. UVa 12563 劲歌金曲(0-1背包)

    https://vjudge.net/problem/UVA-12563 题意: 在一定的时间内连续唱歌,最后一首唱11分钟18秒的劲歌金曲,问最多能长多长时间. 思路: 0-1背包问题,背包容量为t ...

  3. UVa 12563 劲歌金曲 刘汝佳第二版例题9-5;

    Problem J Jin Ge Jin Qu [h]ao (If you smiled when you see the title, this problem is for you ^_^) Fo ...

  4. UVA.10130 SuperSale (DP 01背包)

    UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...

  5. UVA 562 Dividing coins --01背包的变形

    01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostre ...

  6. UVA 624 CD(01背包+输出方案)

    01背包,由于要输出方案,所以还要在dp的同时,保存一下路径. #include <iostream> #include <stdio.h> #include <stri ...

  7. UVA 562 Dividing coins (01背包)

    //平分硬币问题 //对sum/2进行01背包,sum-2*dp[sum/2] #include <iostream> #include <cstring> #include ...

  8. UVA 624 CD(DP + 01背包)

    CD You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music i ...

  9. uva 624 CD (01背包)

      CD  You have a long drive by car ahead. You have a tape recorder, but unfortunately your best musi ...

  10. UVA 624 CD[【01背包】(输出路径)

    <题目链接> 题目大意: 你要录制时间为N的带子,给你一张CD的不同时长的轨道,求总和不大于N的录制顺序 解题分析: 01背包问题,需要注意的是如何将路径输出. 由于dp时是会不断的将前面 ...

随机推荐

  1. transient关键字的含义

    transient java语言的关键字,变量修饰符,如果用transient声明一个实例变量,当对象存储时,它的值不需要维持. Java的serialization提供了一种持久化对象实例的机制.当 ...

  2. The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation. 怎么解决

    cd 文件 pod install --no-repo-update 就可以了

  3. tableview隐藏多余分割线

    - (void)setExtraCellLineHidden: (UITableView *)tableView{ UIView *view =[ [UIView alloc]init]; view. ...

  4. The Havel-Hakimi Algorithm

    1.If any di>=n then fail 2.If there is an odd number of odd degrees then fail 3.If there is a di& ...

  5. PHP上传图片时,如何判断上传的文件是否为可用的图片文件

    利用getimagesize函数: function isImage($filename){$types = '.gif|.jpeg|.png|.bmp';//定义检查的图片类型if(file_exi ...

  6. [MacOS] xcrun: error: active developer path ("/Volumes/Xcode/Xcode6-Beta.app/Contents/Developer") does not exist, use xcode-select to change

    When using MacOS with xcode6-beta, i always meet these error: xcrun: error: active developer path (& ...

  7. windows中的上帝模式开启方法

    在任何地方创建一个新的文件夹 把文件夹命名为"GodMode.{ED7BA470-8E54-465E-825C-99712043E01C}" 回车,ok了

  8. 基于时间点恢复数据库stopat

    create database newtestdb use newtestdbgo drop table t1go create table t1 (id int not null identity( ...

  9. yii2添加自定义字段

    在模型model文件中,添加 public $attributes;即可,$attributes 为要添加的新字段

  10. linux_c学习笔记之curl的使用一

    参考文档 使用libcurl发送PUT请求上传数据以及DELETE请求删除数据 http://blog.163.com/lixiangqiu_9202/blog/static/535750372014 ...