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

状态dp[i][j]表示前i首歌曲,恰好唱的时间为j时,所唱的最长时间,于是就是裸的01背包了。

/*----UVa12563 Jin Ge Jin Qu
*/
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<vector>
#include<string.h>
#include<math.h>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 50 + 10; int dp[maxn*180+678]; //dp[i][j]表示前i首歌曲,时间长度恰好为j,所唱的最多曲数,可以直接使用一维数组
int t[maxn]; int main(){
int i, j,T,iCase=1,n,m;
scanf("%d", &T);
while (T--){
scanf("%d%d", &n, &m);
m--; //在此不能恰好唱完,因为后面还要唱一首jin Ge Jin Qu
for (i = 1; i <= n; i++)scanf("%d", &t[i]); for (i = 0; i <= m; i++)dp[i] = -INF;
dp[0] = 0; for (i = 1; i <= n; i++){
for (j = m;j>=t[i];j--)
dp[j]=max(dp[j], dp[j - t[i]] + 1);
}
int ans =-INF, maxt = -INF;
for (j = m; j >=0; j--)
if (ans < dp[j]){ ans = dp[j];maxt = j;} printf("Case %d: %d %d\n",iCase++,ans + 1, maxt + 678);
}
return 0;
}

  

uVa 12563 Jin Ge Jin Qu的更多相关文章

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

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

  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. 一道令人抓狂的零一背包变式 -- UVA 12563 Jin Ge Jin Qu hao

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

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

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

  7. UVa 12563 Jin Ge Jin Qu hao【01背包】

    题意:给出t秒时间,n首歌分别的时间a[i],还给出一首长度为678的必须唱的劲歌金曲,问最多能够唱多少首歌(只要最后时间还剩余一秒,都可以将劲歌金曲唱完) 用dp[i]代表花费i时间时唱的歌的最大数 ...

  8. UVA 12563 Jin Ge Jin Qu hao

    dp-背包 开始用普通dp写了一发发现没法确定最大时间... 后来看到大牛机智的写法,嗯...dp表示当前状态能否成立:然后从条件最好的状态开始遍历,直到这个状态成立然后退出遍历. 具体的看代码吧.. ...

  9. UVA - 12563 Jin Ge Jin Qu hao (01背包变形)

    此题应该注意两个点,首先背包容量应该缩减为t-1,因为最长的歌不超过三分钟,而劲歌金曲有678s,所以肯定要留出这个时间来.其次注意优先级,保证唱的歌曲数目最多,在此前提下尽可能的延长时间. 处理方法 ...

随机推荐

  1. PAT 1075 链表元素分类

    https://pintia.cn/problem-sets/994805260223102976/problems/994805262953594880 给定一个单链表,请编写程序将链表元素进行分类 ...

  2. 【bzoj3675】[Apio2014]序列分割 斜率优化dp

    原文地址:http://www.cnblogs.com/GXZlegend/p/6835179.html 题目描述 小H最近迷上了一个分隔序列的游戏.在这个游戏里,小H需要将一个长度为n的非负整数序列 ...

  3. 【Luogu】P2490黑白棋(博弈DP)

    题目链接 题解链接 #include<cstdio> #include<cstring> #include<algorithm> #include<cstdl ...

  4. latex公式测试

    https://katex.org/docs/supported.html 1.x_n $x_n$ 2 . \sum $\sum$ \sum_{i=1}^n $\sum_{i=1}^n $ 3.\bi ...

  5. fastjson对json字符串JSONObject和JSONArray互相转换操作示例

    2017-03-25 直接上代码: package com.tapt.instance; import com.alibaba.fastjson.JSON; import com.alibaba.fa ...

  6. Python实现队列

    队列的数据结构的主要结构:一个结点类和两个方法:出队列和进队列 class Node(object): def __init__(self,val): self.val = val self.next ...

  7. 转 android Listview,gridview局部刷新,部分刷新 (仿QQ动态评论局部刷新)

    转自:http://blog.csdn.net/jdsjlzx/article/details/45582719 众所周知Listview和Gridview的刷新界面的方式是调用adapter.not ...

  8. nutch2.3.1源码分析——InjectorJob

    InjectorJob实现的功能是:从种子站点文件当中读取站点信息并且将这些站点的个数.url(url以 域名:协议/端口号/路径名 设为形式存储在数据库当中,为了提高读写速度)回写到Context类 ...

  9. POJ 2676 数独+dfs深搜

    数独 #include "cstdio" #include "cstring" #include "cstdlib" #include &q ...

  10. img 标签下多余空白的解决方法

    在浏览器中,图片默认的vertical-align是baseline.那么,我们该如何去掉这多余的空白呢? 1)将图片转换为块级 img{display:block;} 2) 设置图片的垂直对齐方式 ...