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. struts2_Action的三种实现方式

    1.普通java类 package com.ahd.action; public class HelloAction{ public String execute() throws Exception ...

  2. python_字符串的操作

    一:字符串的方法与操作 *注意:首字母为l的为从左边操作,为r的方法为从右边操作 1.__contains__()判断是否包含 判断指定字符或字符串是否包含在一个字符串内,返回值为true或者fals ...

  3. Java中的方法重载与方法重写

    重载(overload) 重载是在一个类中,同名方法拥有不同的参数列表则视为重载.不同的参数列表包括:参数数量不同,参数类型不同,参数顺序不同.重载对于返回类型没有要求,所以不能通过返回类型去判断是否 ...

  4. Hibernate入门(三)

    持久化类的编写规则: 1.持久化类需要提供无参的构造方法.因为在Hibernate的底层需要使用反射生成的实例. 2.持久化类的属性需要私有,对私有的属性提供共有的get和set方法.因为在Hiber ...

  5. 浅析多线程的对象锁和Class锁

    一.前言 本来想在另外一篇文章说的,发现可能篇幅有点大,所以还是另开一篇博文来说好了.知识参考<Java多线程编程核心技术>,评价下这本书吧——大量的代码,简单的说明,真像在看博客.不过这 ...

  6. Flask 系列之 Pagination

    说明 操作系统:Windows 10 Python 版本:3.7x 虚拟环境管理器:virtualenv 代码编辑器:VS Code 实验目标 实现当前登录用户的事务浏览.添加.删除 操作 实现 首先 ...

  7. Eureka开启登录认证

    Eureka服务端配置 一.Eureka的pom.xml 引入spring-boot-starter-security坐标 <dependency> <groupId>org. ...

  8. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/fs/CanUnbuffer

    在执行spark on hive 的时候在  sql.show()处报错 : Exception in thread "main" java.lang.NoClassDefFoun ...

  9. 微信小程序性能优化技巧

    摘要: 如果小程序不够快,还要它干嘛? 原文:微信小程序性能优化方案--让你的小程序如此丝滑 作者:杜俊成要好好学习 Fundebug经授权转载,版权归原作者所有. 微信小程序如果想要优化性能,有关键 ...

  10. 转:敏捷开发之Scrum扫盲篇

    现在敏捷开发是越来越火了,人人都在谈敏捷,人人都在学习Scrum和XP... 为了不落后他人,于是我也开始学习Scrum,今天主要是对我最近阅读的相关资料,根据自己的理解,用自己的话来讲述Scrum中 ...