这是一道不错的题。首先通过分析,贪心法不可取,可以转化为01背包问题。但是这过程中还要注意,本题中的01背包问题要求背包必须装满!这就需要在普通的01背包问题上改动两处,一个是初始化的问题:把dp[0]初始化为0,而其它的dp值都初始化为-1,表示不符要求。为什么这么初始化?背包九讲里说到:“初始化的dp数组事实上就是在没有任何物品可以放入背包时的合法状态。如果要求背包恰好装满,那么此时只有容量为0的背包可能被价值为0的nothing“恰好装满”,其它容量的背包均没有合法的解,属于未定义的状态,它们的值就都应该是-1了。如果背包并非必须被装满,那么任何容量的背包都有一个合法解“什么都不装”,这个解的价值为0,所以初始时状态的值也就全部为0了。”还需要改动一个地方是:if判断语句中要加一项:dp[j-a[i]]>=0,这样才能在更新数组时保证在背包符合满载的条件下去更新数组。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<list>
#include<deque>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<cctype>
#include<sstream>
using namespace std;
#define pii pair<int,int>
#define LL long long int
const double eps=1e-;
const int INF=;
const int maxn=+;
int T,n,t,a[maxn],dp[*maxn];
int main()
{
//freopen("in2.txt","r",stdin);
scanf("%d",&T);
for(int cas=; cas<=T; cas++)
{
memset(dp,-,sizeof(dp));
dp[]=;
scanf("%d%d",&n,&t);
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
}
int MAX=*n;
for(int i=; i<=n; i++)
{
for(int j=MAX; j>=; j--)
{
if(j>=a[i]&&dp[j-a[i]]>=)
{
dp[j]=max(dp[j],dp[j-a[i]]+);
}
}
}
int ans=,sum=;
for(int i=; i<t; i++)
{
if(dp[i]>=ans)
{
ans=dp[i];
sum=i;
}
}
printf("Case %d: %d %d\n",cas,ans+,sum+);
}
return ;
}

uva12563 Jin Ge Jin Qu hao(01背包)的更多相关文章

  1. 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 ...

  2. UVA12563Jin Ge Jin Qu hao(01背包)

    紫书P274 题意:输入N首歌曲和最后剩余的时间t,问在保证能唱的歌曲数目最多的情况下,时间最长:最后必唱<劲歌金曲> 所以就在最后一秒唱劲歌金曲就ok了,背包容量是t-1,来装前面的歌曲 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. uVa 12563 Jin Ge Jin Qu

    分析可知,虽然t<109,但是总曲目时间大于t,实际上t不会超过180*n+678.此问题涉及到两个目标信息,首先要求曲目数量最多,在此基础上要求所唱的时间尽量长.可以定义 状态dp[i][j] ...

  7. UVa 12563 (01背包) Jin Ge Jin Qu hao

    如此水的01背包,居然让我WA了七次. 开始理解错题意了,弄反了主次关系.总曲目最多是大前提,其次才是歌曲总时间最长. 题意: 在KTV房间里还剩t秒的时间,可以从n首喜爱的歌里面选出若干首(每首歌只 ...

  8. Jin Ge Jin Qu hao UVA - 12563 01背包

    题目:题目链接 思路:由于t最大值其实只有180 * 50 + 678,可以直接当成01背包来做,需要考虑的量有两个,时间和歌曲数,其中歌曲优先级大于时间,于是我们将歌曲数作为背包收益,用时间作为背包 ...

  9. 一道令人抓狂的零一背包变式 -- UVA 12563 Jin Ge Jin Qu hao

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  10. 【紫书】(UVa12563)Jin Ge Jin Qu hao

    继续战dp.不提. 题意分析 这题说白了就是一条01背包问题,因为对于给定的秒数你只要-1s(emmmmm)然后就能当01背包做了——那1s送给劲歌金曲(?).比较好玩的是这里面dp状态的保存——因为 ...

随机推荐

  1. 【转】Python爬虫_示例

    爬虫项目:爬取汽车之家新闻资讯   # requests+Beautifulsoup爬取汽车之家新闻 import requests from bs4 import BeautifulSoup res ...

  2. vlc做转发的命令

    from:http://blog.csdn.net/linyuejiang/article/details/7498482 将一个udp的多播流转发复制到http流中去 vlc udp://@239. ...

  3. val() attr('value')

    val() 只能更改输入框内的值,能更改value属性, 在浏览器中体现不出value被改变 attr('value') 都可以 谷歌浏览器 val,attr都能获取输入框最新的value值

  4. matlab fread

    Matlab中fread函数用法    “fread”以二进制形式,从文件读出数据. 语法1:[a,count]=fread(fid,size,precision) 语法2:[a,count]=fre ...

  5. 【读书笔记】《Java Web整合开发实践》第3章 JSP

    1. JSP:Java Server Pages 2. JSP注释:<%--注释内容--%> 3. page指令(页面指令):定义JSP页面的全局属性. <%@ page langu ...

  6. 【leetcode刷题笔记】Text Justification

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  7. 【Head First Servlets and JSP】笔记18:JSP指令

    mark. jetbrain tomcat配置:https://www.jetbrains.com/help/idea/2017.1/creating-and-running-your-first-w ...

  8. 【Head First Servlets and JSP】笔记17:JSP所生成的servlet相关问题

    1.容器根据你所写的JSP生成一个类, /* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat ...

  9. Java 中的会话管理—— HttpServlet,Cookies,URL Rewriting(转)

    索引 1.什么是 Session? 2.Java 中的会话管理—— Cookie 3.Java Servlet 中的 Session —— HttpSession 理解 JSESSIONID Cook ...

  10. 生产&消费者模型

    import queue,threading,time ,random q = queue.Queue() def producer(): count = 1 while count <11: ...