转载请注明出处: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. 【AC自动机/fail树】BZOJ3172- [Tjoi2013]单词

    [题目大意] http://www.lydsy.com:808/JudgeOnline/problem.php?id=3172 某人读论文,一篇论文是由许多单词组成.但他发现一个单词会在论文中出现很多 ...

  2. [CSAcademy]Connected Tree Subgraphs

    题目大意: 给你一棵n个结点的树,求有多少种染色方案,使得染色过程中染过色的结点始终连成一块. 思路: 树形DP. 设f[x]表示先放x时,x的子树中的染色方案数,y为x的子结点. 则f[x]=pro ...

  3. fullPage全屏滚动的实现

    fullPage.js 是一个基于 jQuery 的插件,它能够很方便.很轻松的制作出全屏网站. 用法: 1.引入jquery 2.引入fullPage 3.每个section代表一屏 4.js启动: ...

  4. Linux-C网络编程之epoll函数

    上文中说到假设从100的不同的地方取外卖,那么epoll相当于一部手机,当外卖到达后,送货员能够通知你.从而达到每去必得,少走非常多路. 它是怎样实现这些作用的呢? epoll的功能 epoll是se ...

  5. iOS:CocoaPods详解

    原文地址:http://blog.csdn.net/wzzvictory/article/details/18737437 一.什么是CocoaPods 1.为什么需要CocoaPods 在进行iOS ...

  6. 利用saltstack的event实现自己的功能

    saltstack的master上minion连接较多,下面这个程序可以分析哪些minion任务执行成功,哪些执行失败以及哪些没有返回. 脚本说明: 一.最先打印出本次任务的job id.comman ...

  7. Solr6.6.0 用 SimplePostTool与界面dataimport索引方式区别

    通过测试发现用SimplePostTool与solr界面dataimport索引数据的结果有如下区别: 1.SimplePostTool索引数据对结构化数据文件索引比较合适,比如csv/json/xm ...

  8. 项目打jar包,怎么把第三放jar包一起打入

    <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> < ...

  9. 64位系统注冊32位的directshow filter文件

    在SERVER2008上注冊自己写的directshow filter 的dll或者ax文件的时候总是提示 [Window Title] RegSvr32 [Content] 模块".\ba ...

  10. [转]SQL Server 性能调优(内存)

      存储引擎自调整 sql server 是如何分配内存的 32bit地址空间的限制 用户模式vas分配和virtualalloc 非boffer pool 分配内存(保留内存) VAS调整 AWE ...