Problem UVA12563-Jin Ge Jin Qu hao

Accept: 642  Submit: 7638
Time Limit: 3000 mSec

Problem Description

(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 minutes and 18 seconds) — I know that there are Jin Ge Jin Qu II and III, and some other unofficial versions. But in this problem please forget about them.
Why is it popular? Suppose you have only 15 seconds left (until your time is up), then you should select another song as soon as possible, because the KTV will not crudely stop a song before it ends (people will get frustrated if it does so!). If you select a 2-minute song, you actually get 105 extra seconds! ....and if you select Jin Ge Jin Qu, you’ll get 663 extra seconds!!! Now that you still have some time, but you’d like to make a plan now. You should stick to the following rules:
• Don’t sing a song more than once (including Jin Ge Jin Qu). • For each song of length t, either sing it for exactly t seconds, or don’t sing it at all. • When a song is finished, always immediately start a new song.
Your goal is simple: sing as many songs as possible, and leave KTV as late as possible (since we have rule 3, this also maximizes the total lengths of all songs we sing) when there are ties.

Input

The first line contains the number of test cases T (T ≤ 100). Each test case begins with two positive integers n, t (1 ≤ n ≤ 50, 1 ≤ t ≤ 109), the number of candidate songs (BESIDES Jin Ge Jin Qu) and the time left (in seconds). The next line contains n positive integers, the lengths of each song, in seconds. Each length will be less than 3 minutes — I know that most songs are longer than 3 minutes. But don’t forget that we could manually “cut” the song after we feel satisfied, before the song ends. So here “length” actually means “length of the part that we want to sing”.
It is guaranteed that the sum of lengths of all songs (including Jin Ge Jin Qu) will be strictly larger than t.

 Output

For each test case, print the maximum number of songs (including Jin Ge Jin Qu), and the total lengths of songs that you’ll sing.
Explanation: In the first example, the best we can do is to sing the third song (80 seconds), then Jin Ge Jin Qu for another 678 seconds. In the second example, we sing the first two (30+69=99 seconds). Then we still have one second left, so we can sing Jin Ge Jin Qu for extra 678 seconds. However, if we sing the first and third song instead (30+70=100 seconds), the time is already up (since we only have 100 seconds in total), so we can’t sing Jin Ge Jin Qu anymore!
 

 Sample Input

2
3 100
60 70 80
3 100
30 69 70
 

Sample Output

Case 1: 2 758

Case 2: 3 777

题解:普通的01背包,这里状态的定义为到第t秒最多唱几首歌,而不是t秒内最多,因此初始化注意一下,除了dp[0]=0,其余都要定义成一个不可能被转移过去的值(大的负数就好),背包九讲里有讲解,对于前0首歌,只有0秒唱0首歌是一个合法的状态,其余都是不合法的,因此不能让他们转移过去。

 #include <bits/stdc++.h>

 using namespace std;

 int read() {
int q = , f = ;
char ch = ' ';
while (ch < '' || '' < ch) {
if (ch == '-') f = -;
ch = getchar();
}
while ('' <= ch && ch <= '') {
q = q * + ch - '';
ch = getchar();
}
return q * f;
} const int maxn = + , maxt = + ;
const int INF = 0x3f3f3f3f; int n, t;
int T = ;
int ti[maxn], dp[maxt]; int main()
{
//freopen("input.txt", "r", stdin);
int iCase;
iCase = read();
while (iCase--) {
n = read(), t = read();
t--;
int sum = ;
for (int i = ; i < n; i++) {
ti[i] = read();
sum += ti[i];
} for (int i = ; i <= t; i++) {
dp[i] = -INF;
}
dp[] = ;
for (int i = ; i < n; i++) {
for (int j = t; j >= ti[i]; j--) {
dp[j] = max(dp[j], dp[j - ti[i]] + );
}
} int ans = , last = ;
for (int i = ; i <= t; i++) {
if (ans <= dp[i]) {
ans = dp[i];
last = i;
}
}
last += ;
printf("Case %d: %d %d\n", T++, ans + , last);
}
return ;
}

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

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

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

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

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

  7. uva12563 Jin Ge Jin Qu hao(01背包)

    这是一道不错的题.首先通过分析,贪心法不可取,可以转化为01背包问题.但是这过程中还要注意,本题中的01背包问题要求背包必须装满!这就需要在普通的01背包问题上改动两处,一个是初始化的问题:把dp[0 ...

  8. 洛谷 UVA12563 Jin Ge Jin Qu hao 题解

    这道题其实是一道01背包的变形题,主要思路如下:在不把剩余时间用光的前提下(剩余时间>0),尽可能的多唱歌.于是我们可以用dp[i]表示的是到当前i秒时,最多可以唱多少歌. 状态转换方程:dp[ ...

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

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

  10. 【UVa 12563】Jin Ge Jin Qu hao

    [Link]: [Description] KTV给你T秒的唱歌时间; 你有n首一定要唱的歌; 然后有一首很变态的歌有678s,你想在T秒结束之前唱一下这首歌; 因为这样的话,你能尽量晚地走出KTV( ...

随机推荐

  1. 2015 Multi-University Training Contest 6 solutions BY ZJU(部分解题报告)

    官方解题报告:http://bestcoder.hdu.edu.cn/blog/2015-multi-university-training-contest-6-solutions-by-zju/ 表 ...

  2. JUnit4注解

    今天学习了下,mybatis中开发dao的方法,用到了JUnit4进行单元测试, 将JUnit4中的注解总结了下,供大家参考学习: JUnit 4 开始使用 Java 5 中的注解(annotatio ...

  3. C# dev SearchLookUpEdit 和 RepositoryItemSearchLookUpEdit 测试

    一.searchLookUpEdit 绑定数据源 DataTable DtCompany = new DataTable();//数据源表,自己写数据. searchLookUpEditCus_no. ...

  4. 前端整理——css部分

    (1)盒模型(普通盒模型.怪异盒模型) 1.元素的content(内容).padding(内边距).border(边框).margin(外边距)构成了CSS盒模型 2.IE盒模型和W3C盒模型 1)I ...

  5. 2018-09-06 Java实现英汉词典API初版发布在Maven

    在打算批量代码汉化工具 · Issue #86 · program-in-chinese/overview时, 发现没有现成的Java库实现英汉查询功能. 于是开此项目. 源码库: program-i ...

  6. android开发——Android开发中的47个小知识

    1.判断sd卡是否存在  boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environme ...

  7. C# 实现截图软件功能

    本文是利用C# 开发截图软件的小例子,以供学习分享使用. 思路: 截取屏幕图片. 获取要截取的范围,即左上角,右下角坐标 填充到PictureBox中. 笔触功能,荧光笔,矩形,橡皮擦,复制,保存功能 ...

  8. 添加/删除/修改Windows 7右键的“打开方式”

    右键菜单添加/删除"打开方式" 此"打开方式"非系统的"打开方式",二者可以并存. 右键菜单添加"打开方式" 在HKEY ...

  9. Okhttp3请求网络开启Gzip压缩

    前沿 首先OkHttp3是支持Gzip解压缩的,不过我们要明白,它是支持我们在发起请求的时候自动加入header,Accept-Encoding: gzip,而我们的服务器返回的时候header中有C ...

  10. Android Demo Android ActionBarCompat-ListPopupMenu

    示例显示如何使用v7 appcompat库中的PopupMenu显示弹出式菜单.主界面使用V4支持库的ListFragment显示数据列表,当点击列表子项时,在子项下方弹出下拉菜单,并通过设置setO ...