poj2184 Cow Exhibition(p-01背包的灵活运用)

转载请注明出处:http://blog.csdn.net/u012860063

题目链接:http://poj.org/problem?id=2184

Description

"Fat and docile, big and dumb, they look so stupid, they aren‘t much

fun..."

- Cows with Guns by Dana Lyons

The cows want to prove to the public that they are both smart and fun. In order to do this, Bessie has organized an exhibition that will be put on by the cows. She has given each of the N (1 <= N <= 100) cows a thorough interview and determined two values for
each cow: the smartness Si (-1000 <= Si <= 1000) of the cow and the funness Fi (-1000 <= Fi <= 1000) of the cow.

Bessie must choose which cows she wants to bring to her exhibition. She believes that the total smartness TS of the group is the sum of the Si‘s and, likewise, the total funness TF of the group is the sum of the Fi‘s. Bessie wants to maximize the sum of TS
and TF, but she also wants both of these values to be non-negative (since she must also show that the cows are well-rounded; a negative TS or TF would ruin this). Help Bessie maximize the sum of TS and TF without letting either of these values become negative.

Input

* Line 1: A single integer N, the number of cows

* Lines 2..N+1: Two space-separated integers Si and Fi, respectively the smartness and funness for each cow.

Output

* Line 1: One integer: the optimal sum of TS and TF such that both TS and TF are non-negative. If no subset of the cows has non-negative TS and non- negative TF, print 0.

Sample Input

5
-5 7
8 -6
6 -3
2 1
-8 -5

Sample Output

8

Hint

OUTPUT DETAILS:

Bessie chooses cows 1, 3, and 4, giving values of TS = -5+6+2 = 3 and TF

= 7-3+1 = 5, so 3+5 = 8. Note that adding cow 2 would improve the value

of TS+TF to 10, but the new value of TF would be negative, so it is not

allowed.

Source

USACO 2003 Fall

题意:要求从N头牛中选择若干头牛去參加比赛,如果这若干头牛的智商之和为sumS,幽默度之和为sumF现要求在全部选择中。在使得sumS>=0&&sumF>=0的基础上。使得sumS+sumF最大并输出其值.

思路:事实上这道题和普通0-1背包几乎相同,仅仅是要转换下思想。就是在求fun[j]中能够达到的最大smart。

我们设智商属性为费用。幽默感属性为价值,问题转换为求费用大于和等于0时的费用、价值总和。

代码+解释例如以下:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <climits>
#include <ctype.h>
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define PI acos(-1.0)
#define INF 0x3fffffff
int MAX(int a,int b)
{
	if( a > b)
		return a;
	return b;
}
int main()
{
	int s[147],f[147],dp[200047];
	int N,i,j;
	while(~scanf("%d",&N))
	{
		for(i = 0 ; i <= 200000 ; i++)
		{
			dp[i] =-INF;
		}
		for(i = 1 ; i <= N ; i++)
		{
			scanf("%d%d",&s[i],&f[i]);
		}
		dp[100000] = 0;//初始化dp[100000]相当于“dp[0]”也就是智力和为零的时候
		for(i = 1 ; i <= N ; i++)
		{
			if(s[i] < 0 && f[i] < 0)
				continue;
			if(s[i] > 0)//假设s[i]为正数。那么我们就从大的往小的方向进行背包
			{
				for(j = 200000 ; j >= s[i] ; j--)//100000以上是智力和为正的时候
				{
					if(dp[j-s[i]] > -INF)
					{
						dp[j]=MAX(dp[j],dp[j-s[i]]+f[i]);
					}
				}
			}
			else//假设s[i]为负数。那么我们就从小的往大的方向进行背包
			{
				for(j = s[i] ; j <= 200000+s[i] ; j++)//100000一下是当智力和为负的时候
				{
					if(dp[j-s[i]] > -INF)
					{
						dp[j]=MAX(dp[j],dp[j-s[i]]+f[i]);
					}
				}
			}
		}
		int ans=-INF;
		for(i = 100000 ; i <= 200000 ; i++)//仅仅在智力和为正的区间查找智力和幽默值的和最大的
		{
			if(dp[i]>=0)
			{
				ans=MAX(ans,dp[i]+i-100000);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}
时间: 2024-10-10 05:19:46

poj2184 Cow Exhibition(p-01背包的灵活运用)的相关文章

POJ2184 Cow Exhibition 【01背包】

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9383   Accepted: 3601 Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with Guns by Dana Lyons The cows want to prove to t

poj 2184 Cow Exhibition(01背包)

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10882   Accepted: 4309 Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with Guns by Dana Lyons The cows want to prove to

poj(2184)——Cow Exhibition(01背包变形)

其实我想说这道题我觉得我自己并没有深刻的理解.但是今天做了一下,先把现在的想法记录下来 . 题目的大致意思是: 有N头牛,每头牛都有一个s值代表智商值,f值代表它的幽默值. 然后问你智商值和幽默值的总和值最大是多少,其中必须保证智商值的和与幽默值的和为非负数. 一开始我想到的也是01背包,但是这里还有负值,于是我就没有办法了.于是学习到了一个相当于把坐标平移的方法. 因为这里有-1000到0的值,于是我们把它们全都移到右边去,于是变成了非负值0-2000. 解法: 01背包. 但是要注意的是当x

POJ 2184 Cow Exhibition (变种01背包)

题意:有一些奶牛,他们有一定的s值和f值,这些值有正有负,最后让保证s的和为非负且f的和为非负的情况下,s+f的最大值. 思路:很明显的就是取与不取的问题,对于这类问题的第一想法就是背包,但是这道题目很明显与一般的背包不同,因为有负数,但是联想到以前也有这种将负数存入下标的情况,那就是将数组开大,换一种存法 我们用dp[i]存放每个s[i]能得到的最佳F,那么我们就可以根据s[i]的取值采取两种不同的01背包取法,在取完之后,然后再根据背包的有无再去求得最佳答案即可 #include <stdi

USACO 2003 Fall Orange Cow Exhibition /// 负数01背包 oj22829

题目大意: 输入n 接下来n行 每行输入 a b 输出n行中 a+b总和最大的同时满足 所有a总和>=0所有b总和>=0的值 负数的01背包应该反过来 w[i]为正数时 需要从大往小推 即往0推 w[i]为负数时 同样应该往0推 即与正数反过来 #include <bits/stdc++.h> #define INF 0x3f3f3f3f using namespace std; int s[105],f[105],dp[1000*100*2+5]; int main() { in

POJ 2184:Cow Exhibition(01背包变形)

题意:有n个奶牛,每个奶牛有一个smart值和一个fun值,可能为正也可能为负,要求选出n只奶牛使他们smart值的和s与fun值得和f都非负,且s+f值要求最大. 分析: 一道很好的背包DP题,我们将smart值当作物品的体积,将fun值当作物品的价值,每个物品只能取一次,我们求对于每个背包体积求恰好装满该体积时价值和的最大值,也就是当所选奶牛smart值为某个值时,fun值的和的最大值.然后对于每个非负背包体积(smart值的和),判断对应最大价值(fun值的和)是否非负,如果非负说明这是一

poj2184 Cow Exhibition p-01背包的灵活运用

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://poj.org/problem?id=2184 Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with Guns by Dana Lyons The cows want to prove to the public that they ar

poj2184 Cow Exhibition

思路: dp+滚动数组. 类似01背包. 实现: 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 const int INF = 0x3f3f3f3f; 7 const int t = 100000; 8 int a[105], b[105], dp[2][200005], n, p, q; 9 int solve(int n) 10

POJ2184Cow Exhibition (01背包变形)

思路一下子就想到了,转移方程却没想好,看到网上一个的思路相同的代码,改的转移方程. 同时dp全部初始化为负无穷,需要注意一下. AC代码如下: /************************************************** Memory: 1884 KB Time: 250 MS Language: G++ Result: Accepted **************************************************/ #include <iost