uva12563 Jin Ge Jin Qu hao(01背包)

这是一道不错的题。首先通过分析,贪心法不可取,可以转化为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-10;
const int INF=1000000000;
const int maxn=50+5;
int T,n,t,a[maxn],dp[180*maxn];
int main()
{
    //freopen("in2.txt","r",stdin);
    scanf("%d",&T);
    for(int cas=1; cas<=T; cas++)
    {
        memset(dp,-1,sizeof(dp));
        dp[0]=0;
        scanf("%d%d",&n,&t);
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
        }
        int MAX=180*n;
        for(int i=1; i<=n; i++)
        {
            for(int j=MAX; j>=0; j--)
            {
                if(j>=a[i]&&dp[j-a[i]]>=0)
                {
                    dp[j]=max(dp[j],dp[j-a[i]]+1);
                }
            }
        }
        int ans=0,sum=0;
        for(int i=0; i<t; i++)
        {
            if(dp[i]>=ans)
            {
                ans=dp[i];
                sum=i;
            }
        }
        printf("Case %d: %d %d\n",cas,ans+1,sum+678);
    }
    return 0;
}

时间: 2024-07-30 20:31:48

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

UVA 12563 Jin Ge Jin Qu hao 01背包变形

基本的01背包,更新的时候保持背包里每一个元素的num最大然后time尽量长 CSDN也支持makedown了试一下 12563 Jin Ge Jin Qu hao (If you smiled when you see the title, this problem is for you ^_^) For those who don't know KTV, see: http://en.wikipedia.org/wiki/Karaoke_box There is one very popul

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

题意  你在KTV还剩t秒钟的时间  你需要在n首歌中选择尽量多的歌使得歌的数量最多的前提下剩下的时间最小 至少要留一秒给劲歌金曲  所以是一个容量为t-1的01背包   d[i][j]表示恰用j秒时间在前i首歌中最多唱多少首  每个状态有两种选择 唱或不唱第i首歌 所以有转移方程d[i][j]=max(d[i-1][j],d[i-1][j-c[i]]+1) #include <bits/stdc++.h> using namespace std; const int N = 55, M =

UVA 12563 Jin Ge Jin Qu hao(01背包变形:两个背包内容)

题意: KTV里面有n首歌曲你可以选择,每首歌曲的时长都给出了. 对于每首歌曲,你最多只能唱1遍. 现在给你一个时间限制t (t<=10^9) , 问你在最多t-1秒的时间内可以唱多少首歌曲num , 且最长唱歌时间是多少time (time必须<=t-1) ? 最终输出num+1 和 time+678 即可. 注意: 你需要优先让歌曲数目最大的情况下,再去选择总时长最长的. //0 KB 39 ms #include<cstdio> #include<cstring>

UVA12563Jin Ge Jin Qu hao(01背包)

紫书P274 题意:输入N首歌曲和最后剩余的时间t,问在保证能唱的歌曲数目最多的情况下,时间最长:最后必唱<劲歌金曲> 所以就在最后一秒唱劲歌金曲就ok了,背包容量是t-1,来装前面的歌曲,设两个Time求时间,cnt是数量 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; const int MAX

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 don’t know KTV, see: http://en.wikipedia.org/wiki/Karaoke_box There is one very popular song called Jin Ge Jin Qu(). It is a mix of 37 songs, and is e

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, see: http://en.wikipedia.org/wiki/Karaoke_box There is one very popular song called Jin Ge Jin Qu(). It is a mix of 37 songs, and is extremely long (11

uva 12563 - Jin Ge Jin Qu hao(动态规划~劲!歌!金!曲!)

错的我真是无语...还是状态的把握不准确.. 起始状态转移方程是很重要,但是只推出了方程是不够的 对边界状态的处理,对特殊状态的处理,这些都很重要,错了任何一个小地方,都会导致WA.... 细节!更清晰的思路,更全面的考虑! #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n,t; int v[55]; int d[55][10000]; int path[5

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

如此水的01背包,居然让我WA了七次. 开始理解错题意了,弄反了主次关系.总曲目最多是大前提,其次才是歌曲总时间最长. 题意: 在KTV房间里还剩t秒的时间,可以从n首喜爱的歌里面选出若干首(每首歌只能唱一次且如果唱就必须唱完),然后剩下至少1秒的时间来唱那首长678秒的歌曲. 总曲目最多的前提下,尽量使歌曲总时间最长. 分析: 所给时间为t,在t-1秒内进行01背包,num[i]来记录剩余时间为 i 时能长的最多曲目,如果曲目相同还要记录最长时间. 1 //#define LOCAL 2 #i

UVA - 12563 Jin Ge Jin Qu hao (01背包)

首先这题的题意要先读懂,而不是一看到01背包的问题就开始敲. 题目大意: 如果问一个麦霸:"你在KTV里面必唱的歌曲有那些",得到的答案可能会包含古巨基的<劲歌金曲>,一般来说KTV不会在时间到的时候把正在唱的歌给切掉,而是会等它放完,例如还有15秒时再唱一首2分钟的歌,则实际上多唱了105秒.但是唱"神曲"<劲歌金曲>则相当于多唱了663秒. 现在要求你在唱的歌曲的曲数尽量多的情况下,唱的时间尽量长. 思路: 01背包的思路,每次记录下每个

Jin Ge Jin Qu hao

题意:有n首歌,剩余T秒时间,问在结束之前,使得唱的歌的数目尽量多,在此前提下尽量晚的离开KTV.跟多的细节请读Input 题解:如果不考虑数目,则是一道裸的01背包,而现在是数目多的优先,其次再是时间长的优先.所以适合用一个结构体,注意优先级!!! 然后按照01背包的思想做就是了...更新值的时候请注意(不过应该没有人犯我这种错误,还整了3个小时,??) 这次涨知识了,不过有些细节还需要模拟几遍,好好理解一下.加油!!!! 1 #include<cstdio> 2 #include<c