UVA12563 勁歌金曲

題目:勁歌金曲

網址:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=444&page=show_problem&problem=4008php

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

Why is it popular?less

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!!!this

Now that you still have some time, but you’d like to make a plan now. You should stick to the
following rules:spa

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

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 ≤ 10^9
), 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.ci

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

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

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

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

很棒的一道揹包問題。
首先,題目的t數據規模很大,但實際上總曲目的時間要大於t,經過計算能夠知道,t最大爲180 * n + 678 <= 90000。

而後呢?

而後把時間看做體積,數目爲價值進行0-1揹包。

注意初始化時,因爲要求出最長時間,因此讓dp[1 ~ t]均爲負無窮,dp[0] = 0。(從題目要求想較爲易)

代碼以下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn = 55, maxt = 9000 + 10;
int n, t, time[maxn], dp[maxt];
int main()
{
	int T;
	scanf("%d", &T);
	for(int p = 1; p <= T; ++ p)
	{
		scanf("%d %d", &n, &t);
		int sum = 678;
		for(int i = 1; i <= n; ++ i)
		{
			scanf("%d", &time[i]);
			sum += time[i];
		}
		if(t >= sum)
		{
			printf("Case %d: %d %d\n", p, n, sum);
			continue;
		}
		memset(dp, 0xcf, sizeof(dp));
		dp[0] = 0;
		for(int i = 1; i <= n; ++ i)
		{
			for(int j = t; j >= 0; -- j)
			{
				if(j >= time[i]) dp[j] = max(dp[j], dp[j - time[i]] + 1);
			}
		}
		int ans = -1, cnt = -1;
		for(int i = 677; i; -- i)
		{
			if(t + i - 677 > 0)
			{
				if(ans < dp[t + i - 678] + 1)
				{
					ans = dp[t + i - 678] + 1;
					cnt = t + i;
				}
			}
		}
		printf("Case %d: %d %d\n", p, ans, cnt);
	}
	return 0;
}
相關文章
相關標籤/搜索