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
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
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(动态规划基础)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- uVa 12563 Jin Ge Jin Qu
分析可知,虽然t<109,但是总曲目时间大于t,实际上t不会超过180*n+678.此问题涉及到两个目标信息,首先要求曲目数量最多,在此基础上要求所唱的时间尽量长.可以定义 状态dp[i][j] ...
- 【紫书】(UVa12563)Jin Ge Jin Qu hao
继续战dp.不提. 题意分析 这题说白了就是一条01背包问题,因为对于给定的秒数你只要-1s(emmmmm)然后就能当01背包做了——那1s送给劲歌金曲(?).比较好玩的是这里面dp状态的保存——因为 ...
- uva12563 Jin Ge Jin Qu hao(01背包)
这是一道不错的题.首先通过分析,贪心法不可取,可以转化为01背包问题.但是这过程中还要注意,本题中的01背包问题要求背包必须装满!这就需要在普通的01背包问题上改动两处,一个是初始化的问题:把dp[0 ...
- 洛谷 UVA12563 Jin Ge Jin Qu hao 题解
这道题其实是一道01背包的变形题,主要思路如下:在不把剩余时间用光的前提下(剩余时间>0),尽可能的多唱歌.于是我们可以用dp[i]表示的是到当前i秒时,最多可以唱多少歌. 状态转换方程:dp[ ...
- 一道令人抓狂的零一背包变式 -- UVA 12563 Jin Ge Jin Qu hao
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
- 【UVa 12563】Jin Ge Jin Qu hao
[Link]: [Description] KTV给你T秒的唱歌时间; 你有n首一定要唱的歌; 然后有一首很变态的歌有678s,你想在T秒结束之前唱一下这首歌; 因为这样的话,你能尽量晚地走出KTV( ...
随机推荐
- spring_02工具及接口案例
1.spring工具类:ApplicationContextUtil.java,可以返回加载配置文件的容器对象 package com.ahd.utils; import org.springfram ...
- 史上最全python面试题详解(三)(附带详细答案(关注、持续更新))
38.面向对象深度优先和广度优先是什么? 39.面向对象中super的作用? 40.是否使用过functools中的函数?其作用是什么? Python自带的 functools 模块提供了一些常用的高 ...
- JVM虚拟机深入理解+GC回收+类加载
旭日Follow_24 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/xuri24/article/details/81455449 一,前言 本文章是读了“深入理 ...
- 使用JSDoc自动生成代码文档
译者按: 代码要有规范的注释,遵从jsDoc规则来注释可以生成有用的文档. 原文: Generate docs and host it with JSDoc and GitHub Pages 译者: ...
- HDU3038(KB5-D加权并查集)
How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- C# AESCBC256 与 java AESCBC256 加解密
和某上市公司对接接口,他们试用 java AES CBC PKCS5 256 加解密.网上C# 基本不合适. 注意:C# PKCS7 对应 java PKCS5 /// <summary> ...
- Laravel 5.2 二、HTTP路由、创建控制器 与 资源路由
一.HTTP路由 所有路由都定义在 App\Providers\RouteServiceProvider 类载入的 app/Http/routes.php文件中. 1. 基本路由 简单的 Larave ...
- CSS3布局之多列布局columns详解
columns语法:columns:[ column-width ] || [ column-count ]设置或检索对象的列数和每列的宽度 其中:[ column-width ]:设置或检索对象每列 ...
- Unity3D手机斗地主游戏开发实战(04)_出牌判断大小
之前我们实现了叫地主.玩家和电脑自动出牌主要功能,但是还有个问题,出牌的时候,没有有效性检查和比较牌力大小.比如说,出牌3,4,5,目前是可以出牌的,然后下家可以出任何牌如3,6,9. 问题1:出牌检 ...
- 如何用ABP框架快速完成项目(13) - 用ABP遇到难题项目受阻时如何避免项目延迟
只有一个人在开发ABP, 遇到难题时可以: 最根本的, 简化问题, 不要盖楼式结构 前端优先用VSCode看文档, 后端看官网文档. 看ABP源码/issues 到QQ群和微信群里寻求外援. 我建 ...