如此水的01背包,居然让我WA了七次。

开始理解错题意了,弄反了主次关系。总曲目最多是大前提,其次才是歌曲总时间最长。

题意:

在KTV房间里还剩t秒的时间,可以从n首喜爱的歌里面选出若干首(每首歌只能唱一次且如果唱就必须唱完),然后剩下至少1秒的时间来唱那首长678秒的歌曲。

总曲目最多的前提下,尽量使歌曲总时间最长。

分析:

所给时间为t,在t-1秒内进行01背包,num[i]来记录剩余时间为 i 时能长的最多曲目,如果曲目相同还要记录最长时间。

 //#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn = ;
int a[], dp[maxn], num[maxn]; int main(void)
{
#ifdef LOCAL
freopen("12563in.txt", "r", stdin);
#endif int T;
scanf("%d", &T);
for(int kase = ; kase <= T; ++kase)
{
int n, t;
scanf("%d%d", &n, &t);
t--;
for(int i = ; i < n; ++i)
{
scanf("%d", &a[i]);
if(a[i] > ) a[i] = ;
}
memset(dp, , sizeof(dp));
memset(num, , sizeof(num));
for(int i = ; i < n; ++i)
for(int j = t; j >= a[i]; --j)
{
if(num[j] < num[j-a[i]] + )
{
num[j] = num[j-a[i]] + ;
dp[j] = dp[j-a[i]] + a[i];
}
else if(num[j] == num[j-a[i]] + )
{
dp[j] = max(dp[j], dp[j-a[i]] + a[i]);
}
}
printf("Case %d: %d %d\n", kase, num[t]+, dp[t]+);
} return ;
}

代码君

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

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

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

  2. 紫书 例题 9-5 UVa 12563 ( 01背包变形)

    总的来说就是价值为1,时间因物品而变,同时注意要刚好取到的01背包 (1)时间方面.按照题意,每首歌的时间最多为t + w - 1,这里要注意. 同时记得最后要加入时间为678的一首歌曲 (2)这里因 ...

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

  4. uVa 12563 Jin Ge Jin Qu

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

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

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

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

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

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

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

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

随机推荐

  1. SQL Server数据库事务日志序列号(LSN)介绍

    原文:http://blog.csdn.net/tjvictor/article/details/5251463     日志序列编号(LSN)是事务日志里面每条记录的编号. 当你执行一次备份时,一些 ...

  2. 无法将 flash.display::Sprite@156b7b1 转换为 mx.core.IUIComponent

    无法将 flash.display::Sprite@156b7b1 转换为 mx.core.IUIComponent 在Flex Application里,是不能直接用addChild添加Sprite ...

  3. sqlserver 2008 卸载时提示 “重新启动计算机”失败

    问题:sqlserver 2008 卸载时提示 “重新启动计算机”失败 解决办法: 1.打开注册表:开始->运行: regedit 2.找到HKEY_LOCAL_MACHINE\SYSTEM\C ...

  4. recursion lead to out of memory

    There are two storage areas involved: the stack and the heap. The stack is where the current state o ...

  5. POJ 1922

    #include<iostream>cheng da cai zi 11.21 //#include<stdio.h> #include<math.h> using ...

  6. java基础知识回顾之java Thread类学习(九)--wait和notify区别

    wait和sleep区别:  相同点:调用wait,sleep方法都可以是线程进入阻塞状态,让出cpu的执行权. 不同点:1.sleep必须指定时间,但是wait方法可以指定时间,也可以不指定时间. ...

  7. POJ1442Black Box

    http://poj.org/problem?id=1442 题意 : 题目中对给出的数字有两种操作ADD(I)操作,将ADD括号里的数字 I 加到数列里边去,然后是自动排好序的,每一个数列前边都会有 ...

  8. Android 使用MediaRecorder录音

    package com.example.HyyRecord; import android.app.Activity; import android.content.Intent; import an ...

  9. lintcode :Trailing Zeros 尾部的零

    题目: 尾部的零 设计一个算法,计算出n阶乘中尾部零的个数 样例 11! = 39916800,因此应该返回 2 挑战 O(logN)的时间复杂度 解题: 常用方法: 也许你在编程之美中看到,通过求能 ...

  10. iOS开发--多线程

    前面在<Bison眼中的iOS开发多线程是这样的(二)>一文中讲完了多线程的NSThread,不难发现这种方式的多线程实现起来非常的复杂,为了简化多线程的开发,iOS提供了GCD来实现多线 ...