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

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

题意:要求从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;
}

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

  1. poj2184 Cow Exhibition【01背包】+【负数处理】+(求两个变量的和最大)

    题目链接:https://vjudge.net/contest/103424#problem/G 题目大意: 给出N头牛,每头牛都有智力值和幽默感,然后,这个题目最奇葩的地方是,它们居然可以是负数!! ...

  2. POJ 2184 Cow Exhibition【01背包+负数(经典)】

    POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...

  3. poj 2184 Cow Exhibition(01背包)

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10882   Accepted: 4309 D ...

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

    题目大意: 输入n 接下来n行 每行输入 a b 输出n行中 a+b总和最大的同时满足 所有a总和>=0所有b总和>=0的值 负数的01背包应该反过来 w[i]为正数时 需要从大往小推 即 ...

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

    题意:有n个奶牛,每个奶牛有一个smart值和一个fun值,可能为正也可能为负,要求选出n只奶牛使他们smart值的和s与fun值得和f都非负,且s+f值要求最大. 分析: 一道很好的背包DP题,我们 ...

  6. Cow Exhibition (01背包)

    "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with G ...

  7. PKU--2184 Cow Exhibition (01背包)

    题目http://poj.org/problem?id=2184 分析:给定N头牛,每头牛都有各自的Si和Fi 从这N头牛选出一定的数目,使得这些牛的 Si和Fi之和TS和TF都有TS>=0 F ...

  8. POJ-2184 Cow Exhibition(01背包变形)

    Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10949 Accepted: 4344 Descr ...

  9. POJ2184 Cow Exhibition 背包

    题目大意:已知c[i]...c[n]及f[i]...f[n],现要选出一些i,使得当sum{c[i]}和sum{f[i]}均非负时,sum(c[i]+f[i])的最大值. 以sum(c[i])(c[i ...

随机推荐

  1. [LOJ#2540][PKUWC2018]随机算法(概率DP)

    场上数据很水,比较暴力的做法都可以过90分以上,下面说几个做法. 1. 暴力枚举所有最大独立集,对每个独立集分别DP.复杂度玄学,但是由于最大独立集并不多,所以可以拿90. 2. dp[S][k]表示 ...

  2. Exercise02_07

    import javax.swing.JOptionPane; public class Years { public static void main(String[] args){ String ...

  3. Linux下的echo输出换行符

    echo -e "text1\ntext2" -e表示开启转移字符

  4. easyui datagrid checkbox的相关属性整理

    DataGrid其中与选择,勾选相关 DataGrid属性: singleSelect boolean 如果为true,则只允许选择一行. false ctrlSelect boolean 在启用多行 ...

  5. MVC Movie App

    ylbtech- ASP.NET MVC:MVC Movie App 功能描述:MVC Movie App 2,TechnologyAndTheEnvironment(技术与环境) 操作系统: win ...

  6. 测试网站页面网速的Python脚本

    一.测试网站页面网速脚本 [root@salt ~]# cat check_url.py #!/usr/bin/python # coding: UTF-8 import StringIO,pycur ...

  7. FL2440 rt3070模块ap模式移植

    ---------------------------------------------------------------------------------------------------- ...

  8. Android Studio NDK 新手教程(5)--Java对象的传递与改动

    概述 本文主要Java与C++之间的对象传递与取值.包括传递Java对象.返回Java对象.改动Java对象.以及性能对照. 通过JNIEnv完毕数据转换 Java对象是存在于JVM虚拟机中的,而C+ ...

  9. linux导入so文件

    在linux系统中,有时候会遇到so文件丢失的问题. 此时一个常用的操作是将缺失的so文件拷贝到主机上.然后设置以下环境变量来进行导入 export LD_LIBRARY_PATH=/usr/lib/ ...

  10. non-compatible bean definition of same name and class

    在整合struts2.1.6+spring2.5.6开发中,使用了注解和struts-convention来实现零配置管理.spring也使用注解annotation方式.现在的问题是:我在连个个不同 ...