转载请注明出处: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. 【小笔记】斜率优化的结论(WC)

  2. OC语言基础之NSArray

    0.数组的分类 NSArray :不可变数组 NSMutableArray : 可变数组 1: // @[] 只创建不可变数组NSArray 2: /* 错误写法 3: NSMutableArray ...

  3. 使用FluentValidation来进行数据有效性验证

    之前我介绍过了使用系统自带的Data Annotations来进行数据有效性验证,今天在CodePlex上逛的时候,发现了一个非常简洁好用的库:FluentValidation 由于非常简洁,就直接拿 ...

  4. 最短路径之迪杰斯特拉算法的Java实现

    Dijkstra算法是最短路径算法中为人熟知的一种,是单起点全路径算法.该算法被称为是“贪心算法”的成功典范.本文接下来将尝试以最通俗的语言来介绍这个伟大的算法,并赋予java实现代码. 一.知识准备 ...

  5. 数据实时分析平台 Heron

    Twitter发布了新开发的数据实时分析平台Heron,以下为官方文档摘译: 我们每天在Twitter上处理着数十亿的事件.正如你猜测的那样,实时分析这些事件是一个巨大的挑战.目前,我们主要的分析平台 ...

  6. JavaScriptCore全面解析 (上篇)

    收录待用,修改转载已取得腾讯云授权 作者 | 殷源 编辑 | 迷鹿 殷源,专注移动客户端开发,微软Imagine Cup中国区特等奖获得者,现就职于腾讯. JavaScript越来越多地出现在我们客户 ...

  7. C# 中 in,out,ref 的作用与区别

    In:过程不会改写In的内容 Out和out:传入的值不会被过程所读取,但过程可以写 ref:传入的值,过程会读,也会写 就象你把布料送到裁缝的一个收料箱(裁缝用这个区别是哪家客户) IN:这块布料, ...

  8. Node.js 使用http客户端得到网站的图片下载到本地

    以下代码有些冗余,大家捡核心看就好. // 内置http模块,提供了http服务器和客户端功能 var http=require("http"); // cheerio模块,提供了 ...

  9. 用C++实现文件压缩(1.5)

    今天主要做的就是,将完成huffman编码的数据以二进制的形式写入文件中.这是个挺苦逼的活. 不过好在我以前玩过一段时间的单片机,所有能够较好的实现位运算,一位一位的将数据存放到缓冲区中,然后统一写入 ...

  10. 如何使用FlashgameMaster修改游戏

    1 打开任意一款游戏,注意最好是AS2.0版的,我们以黄金矿工为例. 2 使用方法: 点击编辑按钮的打开编辑面板出现编辑面板后在对话框中输入要查找的数值,点击查找按钮,会列表出所以等于该值的变量. 如 ...