解题报告 之 UVA12563 Partitioning by Palindromes

解题报告 之 UVA12536 Jin Ge Jin Qu hao


Description

Download as PDF

John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, Jo(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.hn must determine the shortest closed tour that connects his destinations. Each destination is represented by a point in the plane pi = < xiyi > . John uses the following strategy: he starts from the leftmost point, then he goes strictly left to right to the rightmost point, and then he goes strictly right back to the starting point. It is known that the points have distinct x -coordinates.

Write a program that, given a set of n points in the plane, computes the shortest closed tour that connects the points according to John's strategy.

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



题目大意:在KTV中,最后一首歌即使到了消费时间也不会掐掉。所以最后一首歌必须唱《劲歌金曲》,长678s。现在还有n首喜欢的歌可以唱其中一些,还剩 t 秒,问在最后一首歌唱《劲歌金曲》的前提下,最多能唱多少歌,如果能唱的歌数一样,则要求拖的时间最长。(n<=50 , 每首歌长度<=180 , t<=10^9)。保证所有歌的长度和大于t。


分析:首先注意到所有歌的长度之和一定大于t,则 t<=180*50+678 。 那么范围就大大减少了。用DP来搞。用C[i][j] 表示用前 i 首歌在剩余时间为 t 的情况下唱的最多歌曲,last[i][j]表示用前 i 首歌在剩余时间为 t 的情况下能够持续的最长时间。那么转移方程就出来了。


if(j>song[i]) C[i][j]=min(C[i-1][j],C[i-1][j-song[i]]+1)。或者C[i-1][j]==C[i-1][j-song[i]]+1情况下,比较last[i-1][j]==last[i-1][j-song[i]]+song[i]就可以了。注意转移要满足>而不是>=,因为还要留时间给劲歌金曲。song[i]表示第i首歌曲持续的时间。


上代码:

#include<iostream>
#include<algorithm>
const int maxt = 200*30 + 700;
using namespace std;

int song[60];
int C[60][maxt];
int last[60][maxt];

int main()
{
	int kase;
	cin >> kase;
	for (int step = 1; step <= kase; step++)
	{
		int n,t;
		cin >> n>>t;
		for (int i = 1; i <= n; i++)
		{
			cin >> song[i];
		}

		for (int i = 1; i <= n; i++)
		{
			for (int j = 0; j <= t; j++)
			{
				C[i][j] = (i == 1 ? 0 : C[i - 1][j]);
				last[i][j] = (i == 1 ? 0 : last[i - 1][j]);
				if (j > song[i])
				{
					if (C[i][j] < C[i - 1][j - song[i]] + 1 || C[i][j] == C[i - 1][j - song[i]] + 1 && last[i][j] < last[i - 1][j - song[i]] + song[i])
					{
						C[i][j] = C[i - 1][j - song[i]] + 1;
						last[i][j] = last[i - 1][j - song[i]] + song[i];
					}
				}
			}
		}
		cout << "Case " << step << ": ";
		cout << C[n][t]+1 <<" "<<last[n][t]+678<< endl;
		
	}
	return 0;
}

啦啦啦啦啦 DP继续推进哦!! 也不知道上周末踩点的费用,队长会不会报销。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值