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.

【题意】给出n头牛，分别给出它们的si，fi；取出几头使得si和fi的和最大，并且si之和与fi之和不能为负数；

【思路】背包题，就是存在负数的情况，可以把负数存入下标，数组开大点。用dp[i]存放每个s[i]能得到的最大的f，根据dp的有无，选出最大的dp

```#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
;
<<;
int dp[N];
],f[];
void init()
{
;i<=;i++)
dp[i]=-inf;
dp[]=;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
init();
;i<=n;i++)
{
scanf("%d%d",&s[i],&f[i]);
}
;i<=n;i++)
{
&&f[i]<) continue;//两个都为负，没有必要进行下去
)//si为正，进行从大到小的背包
{
;j>=s[i];j--)
{
if(dp[j-s[i]]>-inf)
dp[j]=max(dp[j],dp[j-s[i]]+f[i]);
}
}
else//为负数则从小到大背包
{
+s[i];j++)
{
if(dp[j-s[i]]>-inf)
dp[j]=max(dp[j],dp[j-s[i]]+f[i]);
}
}

}
int ans=-inf;
;i<=;i++)```
`//dp[]的范围是100000~200000；i就是s[i]，如果此时dp[i]也就是f[i]大于等于0的话，//再加上s[i]-100000（界限）就是答案`
`; }`

## Cow Exhibition_背包（负数情况）的更多相关文章

1. POJ-2184 Cow Exhibition---01背包变形（负数偏移）

题目链接: https://vjudge.net/problem/POJ-2184 题目大意: 给出num(num<=100)头奶牛的S和F值(-1000<=S,F<=1000),要 ...

2. POJ 2184 01背包+负数处理

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10200   Accepted: 3977 D ...

3. poj 1837 01背包

Balance Time Limit: 1000 MS Memory Limit: 30000 KB 64-bit integer IO format: %I64d , %I64u Java clas ...

4. Margaritas on the River Walk_背包

Description One of the more popular activities in San Antonio is to enjoy margaritas in the park alo ...

5. java判断字符串是否为数字，包括负数

/** * 判断是否为数字,包含负数情况 * @param str * @return */ private boolean isNumeric(String str){ Boolean flag = ...

6. 三：背包DP

01背包问题描述 已知:有一个容量为V的背包和N件物品,第i件物品的重量是weight[i],收益是cost[i]. 限制:每种物品只有一件,可以选择放或者不放 问题:在不超过背包容量的情况下,最多能 ...

7. Leetcode分类刷题答案&amp;心得

Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...

8. java 心得

11. 最后的笑声 package javaBookPractice; public class LastLaugh { public static void main(String[] args) ...

9. No.007：Reverse Integer

问题: Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321 官方难度: Ea ...

## 随机推荐

1. Moon.Orm 5.0(MQL版)分页功能的设计(求指教,邀请您的加入)

一.分页的分类及分析 1)分页的前置条件: 查询的目标条件.第几页.总页数(本质上由查询条件决定).每页条数.请求地址.按照什么字段怎样排序 2)目标结果: 数据列表,List<T>返回 ...

2. hibernate的Criteria条件查询

项目中用到了criteria的查询方式,觉得挺好用的,下班后找了一下资料,一边测试,一边在博客上面记录下来 1.初解 快速浏览了资料,大致了解了以下的内容: 1. Hibernate 定义了Crite ...

3. perl检查变量是否定义

my \$label = defined(\$pieces[0]) ? \$pieces[0] : ""; my @alreadyAddedCol = \$node1->{DB}-& ...

4. [POJ2002]Squares(计算几何，二分)

题目链接:http://poj.org/problem?id=2002 给定一堆点,求这些点里哪些点可以构成正方形,题目给定n<=1000,直接枚举四个点是肯定会超时的,因此要做一些优化. 有公 ...

5. 中文乱码 \$dbh-&gt;do(&quot;SET NAMES utf8&quot;);

use DBI; my \$dbUser='DEVOPS'; my \$user="root"; my \$passwd="kjk123123"; my @arr2= ...

本文由社区会员taonavy分享 截取本区域(self.view): UIGraphicsBeginImageContext(CGSizeMake(self.view.frame.size.width ...

7. DJ_Java_Decompiler新手入门教程

首先声明:这篇文章并不是我原创,只是感觉挺有用处,想跟大家分享一下,所以标注为原创,希望能有更多的朋友可以看到,还请原作者谅解. 昨天大D说让我写下DJ入门的基础,今天写了一大半了,结果不小心把浏览器 ...

8. Struts2中的JSON问题——后台返回JSON字符串到前台

最近做一个项目遇到一个比较棘手的问题,项目后台采用struts2+Hibernate3+Spring3,前台采用ExtJs4.笔者目前仍是一名大二学生吗,后台框架完全是毫无任何基础,从零学,现学现用. ...

9. RFM用户分层模型简介

RFM用户分层模型在实际商业活动的数据分析中运用的还是挺多的,主要用于用户.商品.门店等等的分群和细分层次,分群之后就可以进行定向精准营销和推广以及促活和留存等等的运营活动. RFM是一种用户分层模型 ...

10. JAVA性能优化：35个小细节让你提升java代码的运行效率

代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用,但是, ...