紫书P274

题意:输入N首歌曲和最后剩余的时间t,问在保证能唱的歌曲数目最多的情况下,时间最长;最后必唱《劲歌金曲》

所以就在最后一秒唱劲歌金曲就ok了,背包容量是t-1,来装前面的歌曲,设两个Time求时间,cnt是数量

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int MAX = 10000;
int cnt[MAX],Time[MAX],song[MAX];
int main()
{
int n,t;
int tase,num = 0;
scanf("%d", &tase);
while( tase-- )
{
scanf("%d%d", &n, &t);
for(int i = 1; i <= n; i++)
scanf("%d", &song[i]);
int v = t - 1;
memset(cnt, 0, sizeof(cnt));
memset(Time, 0, sizeof(Time));
for(int i = 1; i <= n; i++)
{
for(int j = v; j >= song[i]; j--)
{
if(cnt[j] < cnt[j - song[i]] + 1) //选择第i个时的数量多
{
cnt[j] = cnt[j - song[i]] + 1;
Time[j] = Time[j - song[i]] + song[i];
}
else if(cnt[j] == cnt[j - song[i]] + 1) //在数量相等的情况下,更新时间
{
if(Time[j] < Time[j - song[i]] + song[i])
Time[j] = Time[j - song[i]] + song[i];
}
}
}
int res =678 + Time[v];
printf("Case %d: %d %d\n",++num, cnt[v] + 1, res);
}
return 0;
}

  

UVA12563Jin 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. UVA12563-Jin Ge Jin Qu hao(动态规划基础)

    Problem UVA12563-Jin Ge Jin Qu hao Accept: 642  Submit: 7638Time Limit: 3000 mSec Problem Descriptio ...

  3. UVa12563- Jin Ge Jin Qu hao

    思路一定要清晰! /* * Author: Bingo * Created Time: 2014/12/25 3:45:35 * File Name: uva12563.cpp */ #include ...

  4. UVA 12563 "Jin Ge Jin Qu hao" (背包)

    传送门 debug了好一会,突然发现,输出错了,emmm......... 明天再写debug历程: (PS:ipad debug是真的繁琐) 题意: 题解: 尽管题干中给的 t 的范围很大,但是 t ...

  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 (01背包) Jin Ge Jin Qu hao

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

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

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

随机推荐

  1. 24Spring_事务管理机制

    第一部分:Spring事务管理高层抽象接口 我们介绍三个接口:1.PlatformTransactionManager 2.TransactionDefinition  3.TransactionSt ...

  2. swift 判断输入的字符串是否为数字

    // 判断输入的字符串是否为数字,不含其它字符 func isPurnInt(string: String) -> Bool { let scan: Scanner = Scanner(stri ...

  3. crontab任务取消发送邮件

    1. 方式一,每一个计划任务后加上 >/dev/null 2>&1 */5 * * * * sh /web/adm/Shell/checkin_user_count_everyda ...

  4. Socket Programming in C#--Multiple Sockets

    Now lets say you have two sockets connecting to either two different servers or same server (which i ...

  5. Discuz X3核心文件解析

    <?php /** *      [Discuz!] (C)2001-2099 Comsenz Inc. *      This is NOT a freeware, use is subjec ...

  6. win7(X64)+VS2013+OpenCV3.1环境配置

    &1 源文件 VS2013: 链接:http://pan.baidu.com/s/1o8EKQq2 密码:open OpenCV3.1:  链接:http://pan.baidu.com/s/ ...

  7. An Introduction to Interactive Programming in Python (Part 1) -- Week 2_1 练习

    # Practice Exercises for Functions # Solve each of the practice exercises below. # 1.Write a Python ...

  8. 【转】其实你不知道MultiDex到底有多坑

    遭遇MultiDex 愉快地写着Android代码的总悟君往工程里引入了一个默默无闻的jar然后Run了一下, 经过漫长的等待AndroidStudio构建失败了. 于是带着疑惑查看错误信息. UNE ...

  9. WPF开发时光之痕日记本

       很久没有写东西了,新的一年新的开始吧. 很早就想自己开发一款日记本软件不仅自己使用,也可以让大家免费使用,最主要的是对自己有一个认可,自学WPF以来,感觉不很顺利,WPF的资料相对来说有点少,主 ...

  10. WCF入门(12)

    前言 上次写是小半年前的事情了,还在原来的公司,还在原来的项目,同时认识了不少人.外包公司总是有些不适应的地方,总在很闲和很忙之间徘徊.凌晨2点被客户电话叫醒,只为copy一个文件从一台服务器到另一台 ...