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;
const int N=;
const int inf=<<;
int dp[N];
int s[],f[];
void init()
{
for(int i=;i<=;i++)
dp[i]=-inf;
dp[]=;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
init();
for(int i=;i<=n;i++)
{
scanf("%d%d",&s[i],&f[i]);
}
for(int i=;i<=n;i++)
{
if(s[i]<&&f[i]<) continue;//两个都为负,没有必要进行下去
if(s[i]>)//si为正,进行从大到小的背包
{
for(int j=;j>=s[i];j--)
{
if(dp[j-s[i]]>-inf)
dp[j]=max(dp[j],dp[j-s[i]]+f[i]);
}
}
else//为负数则从小到大背包
{
for(int j=s[i];j<=+s[i];j++)
{
if(dp[j-s[i]]>-inf)
dp[j]=max(dp[j],dp[j-s[i]]+f[i]);
}
} }
int ans=-inf;
for(int i=;i<=;i++)
//dp[]的范围是100000~200000;i就是s[i],如果此时dp[i]也就是f[i]大于等于0的话,
//再加上s[i]-100000(界限)就是答案
  { 
if(dp[i]>=) ans=max(ans,dp[i]+i-); }
printf("%d\n",ans);
} return ;
}

Cow Exhibition_背包(负数情况)的更多相关文章

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

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

  2. POJ-2184 Cow Exhibition---01背包变形(负数偏移)

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

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

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

  4. 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 ...

  5. LG5196 「USACO2019JAN」Cow Poetry 背包+乘法原理

    \(\mathrm{Cow Poetry}\) 问题描述 LG5196 题解 因为每句诗的长度一定是\(k\),所以自然而然想到背包. 设\(opt[i][j]\)代表到第\(i\)位时,结尾为\(j ...

  6. poj 1837 01背包

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

  7. 背包dp相关

    0/1背包 给出n个物品,每个物品有Vi的价值和Wi的费用,我们总共有m块钱,求最多能得到多少价值的物品. N<=10^3,m<=10^3 记录方案数?记录输出方案? 输出方案: 对每个d ...

  8. Margaritas on the River Walk_背包

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

  9. java判断字符串是否为数字,包括负数

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

随机推荐

  1. java对于文件传输时---编码格式的一些设置方法

    - ----转 读文件: BufferedReader 从字符输入流中读取文本,缓冲各个字符,从而提供字符.数组和行的高效读取. 可以指定缓冲区的大小,或者可使用默认的大小.大多数情况下,默认值就足够 ...

  2. 【待整理】PS切图基础教程

    http://www.w3cfuns.com/article-442-1-1.html http://www.w3cfuns.com/article-443-1-1.html 其他专题研究: floa ...

  3. ARM指令集(上)

    ADuC702x可以用两套指令集:ARM指令集和Thumb指令集.本小节介绍ARM指令集.在介绍ARM指令集之前,先介绍指令的格式. A.2.1  指令格式         (1)基本格式       ...

  4. think in java 读书笔记 2 —— 套接字

    目录 think in java 读书笔记 1 ——移位 think in java 读书笔记 2 —— 套接字 think in java 读书笔记 3 —— 数据报 概要 1. 套接字基本知识 2 ...

  5. Ioc和Aop扩展--多种方式实现依赖注入(构造注入,p命名空间注入,集合类型注入,注入null和注入空值)

    构造注入 语法: <constructor-arg>    <ref bean="bean的id"/> </constructor-arg> 1 ...

  6. DOI EXCEL显示报表

    我这个是比较不规则的数据填充 1.程序开头,定义一个工作区,存对应单元格的值: BEGIN OF TY_EXCEL, C031() TYPE C, C032() TYPE C, C033() TYPE ...

  7. struts2视频学习笔记 09-10(struts2处理流程,指定多个struts配置文件)

    课时9 Struts2的处理流程 StrutsPrepareAndExecuteFilter是Struts 2框架的核心控制器,它负责拦截由<url-pattern>/*</url- ...

  8. MyEclipse生成WAR包并在Tomcat下部署发布[转]

      从来没有想过web项目还能打包的,但是有要求,就不得不去实现,在网上找了一下,发现挺简单的. 首先是使用MyEclipse将web项目打包,如下图所示. 右键选中项目,选择export. 然后选择 ...

  9. oracle Redhat64 安装

    详细可以参考:http://blog.csdn.net/chenfeng898/article/details/8782679 直接执行如下yum安装命令后,如果再出错,跳到2 yum -y inst ...

  10. JavaWeb chapter 1 http协议

    1.  静态web和动态web的区别: 静态web和动态web最本质的区别是静态web是无法进行数据库操作,而动态web是可以进行数据库操作的.动态web的最大特点就是具有交互性,所谓交互性就是服务器 ...