dp-背包

开始用普通dp写了一发发现没法确定最大时间。。。

后来看到大牛机智的写法,嗯。。。dp表示当前状态能否成立;然后从条件最好的状态开始遍历,直到这个状态成立然后退出遍历。

具体的看代码吧。。。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std; int dp[][];
int d[]; int main (){
int T,kase=;
cin>>T;
while (T--){
int n,t;
cin>>n>>t;
for (int i=;i<n;i++){
cin>>d[i];
}
sort (d,d+n);
memset (dp,,sizeof dp);
dp[][]=;
for (int k=;k<n;k++){
for (int j=t;j>=d[k];j--){
for (int i=k+;i>;i--){
if (dp[i-][j-d[k]])
dp[i][j]=;
}
}
}
int ans=;
int time;
int flag=;
for (int i=n;i>=;i--){
for (int j=t-;j>=;j--){//cout<<j<<" ";
if (dp[i][j]){
ans=i;
time=j;
flag=;
break ;
}
}
if (flag)
break ;
}
cout<<"Case "<<++kase<<": "<<ans+<<" "<<time+<<endl;
}
return ;
}

UVA 12563 Jin Ge Jin Qu hao的更多相关文章

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

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

  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. uVa 12563 Jin Ge Jin Qu

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. PHP 获取客户端IP

    function get_ip() { static $realIP; if (isset($_SERVER)){ if (isset($_SERVER["HTTP_X_FORWARDED_ ...

  2. information_schema.partitions 学习

    1.partitions 表中的常用列说明: 1.table_schema:表所在的数据库名 2.table_name:表名 3.partition_method:表分区采用的分区方法 4.parti ...

  3. CSS实现侧边栏固定宽度,内容栏自适应

    1,固定宽度区浮动,自适应区不设宽度而设置 margin 我们拿右边定宽左边自适应来做示范,CSS代码如下: #wrap {    overflow: hidden; *zoom: 1;  }  #c ...

  4. Qt的十六进制的控件

    Qt没有这样的Widget,自己写一个吧.我曾经用MFC写过一个,代码不多,不到2000行,估计用Qt写不到1000行就够了. 可以参考这个qhexedit2 - QHexEdit is a Bina ...

  5. expect set timeout -1 永不超时

    . ~/.bash_profile passwd='xxx' expect <<! set timeout -1 spawn rsync -avH /webapps/Seeyon/A8/b ...

  6. 剑指offer-面试题8.旋转数组的最小数字

    题目:把一个数组最开始的若干个元素搬到数据的末尾,我们称之为 数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组 的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转, ...

  7. LeeCode-Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  8. JDBC官方用法

    JDBC官方用法https://bitbucket.org/xerial/sqlite-jdbc/#markdown-header-usage 代码下载https://github.com/xeria ...

  9. PHP MySQL Insert Into 之 Insert

    向数据库表插入数据 INSERT INTO 语句用于向数据库表添加新记录. 语法 INSERT INTO table_name VALUES (value1, value2,....) 您还可以规定希 ...

  10. svn之——linux下清除svn的用户名和密码

    问题:之前用的svn账号权限不够,需要使用别的账号,所以提出需求——怎么使用新的svn账号进行操作 方法一: linux下删除~/.subversion/auth即可清除之前的用户名和密码:rm -r ...