转载请注明出处: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. [Contest20180314]数列

    数据范围告诉我们要写两档的分 第一档:$M\leq200,N\leq10^9$,可以枚举$m$计算答案 直接矩阵快速幂:$O\left(M^4\log_2N\right)$,会超时,所以我们需要某些“ ...

  2. [CF915F]Imbalance Value of a Tree

    [CF915F]Imbalance Value of a Tree 题目大意: 一棵\(n(n\le10^6)\)个结点的树,每个结点有一个权值\(w_i\).定义\(I(i,j)\)为\(i\)到\ ...

  3. openresty总结

    协程 1.例如当获取的数据没有前后依赖关系时,可以使用ngx.thread.spawn和ngx.thread.wait同时从数据库不同的库.表或者不同来源(mysql,redis等)获取数据. htt ...

  4. INLINE-BLOCK和FLOAT(二)(转)

    一.一抹前言 没有爱的日子,时间如指尖细沙,不知不觉就流逝了.写“CSS float浮动的深入研究.详解及拓展(一)”和“CSS float浮动的深入研究.详解及拓展(二)”似乎就在不久前,然而相隔差 ...

  5. 今天升级Xcode 7.0 bata发现网络访问失败。

    今天升级Xcode 7.0 bata发现网络访问失败.输出错误信息 The resource could not be loaded because the App Transport Securit ...

  6. delphi报警声音 Beep、MessageBeep 和 Windows.Beep

      转自:http://blog.csdn.net/yunqian09/article/details/5554527 我的办法,增加一个timer 设置间隔100ms,通过timer的使能否,控制报 ...

  7. flask-assets使用介绍

    作用:对css.js静态文件进行打包,打包成一个文件,然后去除文件里的换行.空行等进行压缩: 而且 Flask-Assets 还会使用特定的 HTTP Response Header, 让浏览器缓存这 ...

  8. javascript快速入门27--XSLT基础

    XSL 与 XSLT XSL 指扩展样式表语言(EXtensible Stylesheet Language).它主要被用来对XML文档进行格式化,与CSS不同,XSL不仅仅是样式表语言XSL主要包括 ...

  9. 使RESTful Web服务更加实用的10个建议

    提示:随着RESTful Web services的流行程度不断地上升,开发人员需要知道如何避免开发中的陷阱以及让开发出来的Web service达到自己能做到的最好程度. 过去的几年里,我们看到RE ...

  10. MPTCP 源码分析(六) 数据重发

    简述      TCP使用定时器函数tcp_retransmit_timer进行数据重发,MPTCP需要重发数据的时候, 不仅仅在原路径发送数据,而且会在另外一条子路径进行重发.这样考虑的原因是: 考 ...