动态规划 UVA 12563 Jin Ge Jin Qu hao

来源:互联网 发布:软件版权局 编辑:程序博客网 时间:2024/06/04 19:24

题目:

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

3 100

60 70 80 

3 100

30 69 70


Sample Output
Case 1: 2 758 

Case 2: 3 777


这个题目是0-1背包问题的变种。

其实就是相当于留下最后一秒唱劲歌金曲,然后求剩下的时间里面,最多可以唱多少歌,

同时,在保证唱歌数量最多的情况下,求最多可以唱多少时间。

我的思路是考虑前m个歌选n个,在时间t内可以唱多久,递归求解这个问题。

因为是有3个参数的问题,所以需要1个三维数组记录已经计算过的结果。

我的代码:

#include<iostream>#include<string.h>#include<algorithm>using namespace std;int list[55];int times[55][55][9005];int sum;int f(int m, int n, int t)//前m个歌选n个,在时间t内可以唱多久{if (t >= sum)return sum;if (t < 0 || m < n)return -1234567890;if (n == 0)return 0;if (t == 0)return -1234567890;if (m == 1){if (list[1] <= t)return list[1];return -1234567890;}if (times[m][n][t] >= 0)return times[m][n][t];int a = f(m - 1, n, t);int b = f(m - 1, n - 1, t - list[m]) + list[m];times[m][n][t] = (a > b) ? a : b;return times[m][n][t];}int main(){int cas;int n, t;cin >> cas;int num, temp;for (int i = 1; i <= cas; i++){sum = 0;cin >> n >> t;for (int i = 1; i <= n; i++){cin >> list[i];sum += list[i];}sort(list + 1, list + 1 + n);num = 0;temp = t - 1;for (int i = 1; i <= n; i++){temp -= list[i];if (temp < 0)break;num++;}memset(times, -1, sizeof(times));cout << "Case " << i << ": " << num + 1 << " " << f(n, num, t - 1) + 678 << endl;}return 0;}

这个代码应该是没有问题的,我也不确定,但是我测试了很多数据都没有问题。

不过OJ上面通过不了,结果是超时,我感觉应该是效率的问题。

虽然动态规划不会做重复的计算,但是有的时候会有无效的计算。

而且我这个思路不是很好,所以应该会有很多无用的计算。

我的新思路:

完全当做0-1背包问题来处理。

所有歌的总价值为,歌越多价值就越高,歌的数量一定时,总时间约长价值越高。

把价值理解为由数量和时间2个因素综合定义的变量。

注意,这里要用到哈希的思想!

就是说,把一首歌的价值定义为时间+180

代码:

#include<iostream>#include<string.h>#include<algorithm>using namespace std;int list[55];int times[18005];int sum;int main(){int cas;int n, t;cin >> cas;int num, temp;for (int i = 1; i <= cas; i++){sum = 0;cin >> n >> t;for (int i = 1; i <= n; i++){cin >> list[i];sum += list[i];}sort(list + 1, list + 1 + n);num = 0;temp = t - 1;for (int i = 1; i <= n; i++){temp -= list[i];if (temp < 0)break;num++;}cout << "Case " << i << ": " << num + 1 << " ";if (t>sum)cout << sum + 678 << endl;else{for (int i = 1; i <= n; i++)list[i] += 180;//哈希t += (180 * num - 1);memset(times, 0, sizeof(times));for (int i = 1; i <= n; i++){for (int j = t; j >= list[i]; j--){if (times[j] < times[j - list[i]] + list[i])times[j] = times[j - list[i]] + list[i];}}cout << times[t] + 678 - 180 * num << endl;}}return 0;}

可惜wrong answer 仔细一想,还真是的,即使这样定义价值,也不能保证3首歌的价值就一定大于2首歌的价值。

最后,我终于想通了,还是按照0-1背包的方法来刷数组,只不过有2个而已。

代码:

#include<iostream>#include<string.h>using namespace std;int list[55];int num[9005];int times[9005];int sum;int main(){int cas;int n, t;cin >> cas;for (int i = 1; i <= cas; i++){sum = 0;cin >> n >> t;for (int i = 1; i <= n; i++){cin >> list[i];sum += list[i];}cout << "Case " << i << ": ";if (t > sum)cout << n + 1 << " " << sum + 678 << endl;else{memset(num, 0, sizeof(num));memset(times, 0, sizeof(times));for (int i = 1; i <= n; i++){for (int j = t-1; j >= list[i]; j--){if (num[j] == num[j - list[i]] + 1){if (times[j] < times[j - list[i]] + list[i])times[j] = times[j - list[i]] + list[i];}else if (num[j] < num[j - list[i]] + 1){num[j] = num[j - list[i]] + 1;times[j] = times[j - list[i]] + list[i];}}}cout << num[t-1] + 1 << " " << times[t-1] + 678 << endl;}}return 0;}

这个代码是AC了的。





1 0
原创粉丝点击