DP:0-1背包问题

目录

HDU 2602 Bone Collector

HDU 3466 Proud Merchants

UVA 12563 Jin Ge Jin Qu hao


HDU 2602 Bone Collector

题目:

Description

Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave … 
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ? 

Input

The first line contain a integer T , the number of cases. 
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.

Output

One integer per line representing the maximum of the total value (this number will be less than 2  31).

Sample Input

1
5 10
1 2 3 4 5
5 4 3 2 1

Sample Output

14

这个题目只是最简单的0-1背包问题,不说话了,直接上代码

代码:

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
 
int value[1001], volume[1001], ans[1001];
 
int main()
{
	int cas;
	cin >> cas;
	int N, V;
	while (cas--)
	{
		cin >> N >> V;
		memset(ans, 0, sizeof(ans));
		for (int i = 1; i <= N; i++)cin >> value[i];
		for (int i = 1; i <= N; i++)cin >> volume[i];
		for (int i = 1; i <= N; i++)for (int j = V; j >= volume[i]; j--)
			ans[j] = max(ans[j], ans[j - volume[i]] + value[i]);
		cout << ans[V] << endl;
	}
	return 0;
}

HDU 3466 Proud Merchants

题目:

Description

Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerful kingdom in the world. As a result, the people in this country are still very proud even if their nation hasn’t been so wealthy any more. 
The merchants were the most typical, each of them only sold exactly one item, the price was Pi, but they would refuse to make a trade with you if your money were less than Qi, and iSea evaluated every item a value Vi. 
If he had M units of money, what’s the maximum value iSea could get? 

Input

There are several test cases in the input. 
Each test case begin with two integers N, M (1 ≤ N ≤ 500, 1 ≤ M ≤ 5000), indicating the items’ number and the initial money. 
Then N lines follow, each line contains three numbers Pi, Qi and Vi (1 ≤ Pi ≤ Qi ≤ 100, 1 ≤ Vi ≤ 1000), their meaning is in the description. 
The input terminates by end of file marker. 

Output

For each test case, output one integer, indicating maximum value iSea could get. 

Sample Input

2 10
10 15 10
5 10 5
3 10
5 10 5
3 5 6
2 7 3

Sample Output

5
11

这个题目很接近0-1背包问题,有一个地方不同就是,需要q的容量才能装入p大小的东西。

这个差异,我们需要在2个地方进行修改。

第一,刷表的2层for循环,内层循环要以q为界而不是以p为界。

老实说,刚开始我以为只要改这个地方就行了,结果对于题目中给的输入样例 3 10 5 10 5 3 5 6 2 7 3 都算不对。

然后我想了一下,因为0-1背包的刷表的方法是先选后面的物品,再选前面的物品。

虽然对于0-1背包问题,随便什么顺序都是一样的,

但是这个问题里面,先选3 5 6之后,就没法选5 10 5了。

如果先选5 10 5,然后是可以选3 5 6的。

然后顺序问题要怎么样解决呢?

肯定不是对顺序进行枚举!毕竟有500个物品。

这个题目的顺序问题还是不难的,就是计算每个物品的q-p的值,然后以此为序来排序即可。

如果2个物品的q-p是一样的,我们甚至不关心这2个物品的顺序!

具体的原理我也说不太清楚,反正就感觉很显然。

简单的说,装完 i 之后能不能装 j 取决于背包有没有 pi+qj ,装完 j 之后能不能装 i 取决于背包有没有pj+qi

因为2种顺序的总价值是一样的,所以我们应该选择pi+qj和pj+qi里面较小的那个数(对应的那个顺序)。

这也就是先选择q-p较大的物品装入背包的意思。

因为刷表是从后面的物品先装入的,所以物品要按照q-p升序排序。

对于如何寻找出q-p这样的贪心策略,可以看看类似的一个题目 HDU 4864 Task 

代码:

#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
 
int n, m;
struct node
{
	int p;
	int q;
	int v;
};
 
node nod[501];
int r[5001];
 
bool cmp(node a, node b)
{
	return a.q - a.p < b.q - b.p;
}
 
int main()
{
	while (cin >> n >> m)
	{
		for (int i = 1; i <= n; i++)cin >> nod[i].p >> nod[i].q >> nod[i].v;
		sort(nod + 1, nod + n, cmp);
		memset(r, 0, sizeof(r));
		for (int i = 1; i <= n; i++)for (int j = m; j >= nod[i].q; j--)if (r[j] < r[j - nod[i].p] + nod[i].v)r[j] = r[j - nod[i].p] + nod[i].v;
		cout << r[m] << endl;
	}	
	return 0;
}

UVA 12563 Jin Ge Jin Qu hao

题目:

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了的。

猜你喜欢

转载自blog.csdn.net/nameofcsdn/article/details/113099390
今日推荐