Cow Exhibition
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12420   Accepted: 4964

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

---------------------------------------------------------------------------------------------------
f[i][j]表示i头牛,TS为j时TF的值  可能有负值,用一个shift=1e5表示原来的0,ng和ps表示负和正的总和,上下界就有了
愉快地01背包
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=,V=N*,shift=1e5,INF=1e9;
int n,s[N],f[N],ps=,ng=,ans=;//ps+shift ng+shift
int d[V];
void dp(){
for(int i=ng+shift;i<=ps+shift;i++) d[i]=-INF;
d[shift]=;
for(int i=;i<=n;i++){//printf("i %d\n",i);
if(s[i]>=) for(int j=ps+shift;j>=s[i]+ng+shift;j--)
d[j]=max(d[j],d[j-s[i]]+f[i]);//,printf("j %d %d\n",j-shift,d[j]);
if(s[i]<) for(int j=ng+shift;j<=s[i]+ps+shift;j++)
d[j]=max(d[j],d[j-s[i]]+f[i]);//,printf("j %d %d\n",j-shift,d[j]);
}
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){scanf("%d%d",&s[i],&f[i]);if(s[i]>=) ps+=s[i];else ng+=s[i];}
dp();
for(int i=shift;i<=ps+shift;i++) if(d[i]>=) ans=max(ans,d[i]+i-shift);
cout<<ans; //cout<<"\n\n";
//cout<<ps<<" "<<ng;
}

POJ2184 Cow Exhibition[DP 状态负值]的更多相关文章

  1. POJ-2184 Cow Exhibition(01背包变形)

    Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10949 Accepted: 4344 Descr ...

  2. poj2184 Cow Exhibition(p-01背包的灵活运用)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=2184">http://poj.org/problem?id=2184 Descrip ...

  3. poj2184 Cow Exhibition【01背包】+【负数处理】+(求两个变量的和最大)

    题目链接:https://vjudge.net/contest/103424#problem/G 题目大意: 给出N头牛,每头牛都有智力值和幽默感,然后,这个题目最奇葩的地方是,它们居然可以是负数!! ...

  4. POJ 2184 Cow Exhibition (带负值的01背包)

    题意:给你N(N<=100)只牛,每只牛有一个智慧值Si和一个活泼值Fi,现在要从中找出一些来,使得这些牛智慧值总和S与活泼值总和F之和最大,且F和S均为正.Si和Fi范围在-1000到1000 ...

  5. poj2184 Cow Exhibition

    思路: dp+滚动数组. 类似01背包. 实现: #include <iostream> #include <cstdio> #include <algorithm> ...

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

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

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

  8. poj 2184 Cow Exhibition(01背包)

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10882   Accepted: 4309 D ...

  9. POJ 2184 Cow Exhibition (01背包变形)(或者搜索)

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10342   Accepted: 4048 D ...

随机推荐

  1. float 和 inline-block的心得

    float 优点: 横向排列时比较方便,不存在浏览器兼容问题. 缺点: 1)多行横向排行时,换行后经常不能顶行显示. 2)浮动后会跳出当前流,造成父元素高度塌陷,解决办法比较成熟统一 ,不存在浏览器兼 ...

  2. MAVEN中的插件放在哪个dependcies里面

    如果你用maven来管理项目的话,你会发现你要依赖很多plugin,于是引出了一个问题. 一个project中可能有两个<dependcies>这个tag, 如下 <dependci ...

  3. json官方学习档案

    项目经常用json开发,但说实话,对json了解的一直不深入.今天看了下json的官方资料,明了很多. json官方网址:http://www.json.org/json-zh.html JSON(J ...

  4. 接口测试中三种传参请求(Map、request、Integer)解析

    注册企业接口传入的是一个request,查询企业接口传入的是一个integer:根据名称和国家名称模糊匹配接口传入的是一个Map:针对三种不同的传参我怎么作接口测试呢? 1 package com.w ...

  5. HTML5设计网页动态条幅广告(Banner) 已经加上完整源代码

    横幅广告(Banner): 1.横幅广告是网络广告的常见形式,一般位于网页的醒目位置上:当用户单击这些横幅广告时,通常可以链接到相应的广告页面: 2.设计横幅广告时,要力求简单明了,能够体现出主要的中 ...

  6. Java 往年试卷参考答案!!!

    仅供参考: 第一题: E C E A D D C A C A C A B A B C C D B C 第二题: True True False 11 12 13 14 No such file fou ...

  7. 最新中国菜刀caidao-20160620下载和说明

    0x00 中国制造, 黑客之刀 中国菜刀是中国安全圈内使用非常广泛的一款Webshell管理工具,此次老兵大牛进行了更新,界面更加优美,功能更加丰富,有图有真相! 0x01 验证 此次更新还自带了官网 ...

  8. php 操作mysql 分表的一种方法

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARUAAAHXCAIAAAAdrFkKAAAgAElEQVR4nOyd61sTx9//+4fcj+6H95

  9. Helpful Tool

    Remote Connectivity Analyzer(Online) https://testconnectivity.microsoft.com/ https://technet.microso ...

  10. 6、后记:PMO项目管理 - PMO项目管理办公室

    PMO项目管理办公室的作用,按笔者所简化的理解,其作用就是将项目资源标准化.规范化.文档化.然后,根据实际的项目情况,对项目间的资源内容进行协调沟通和培训,让项目组能够更快更好的完成项目建设任务. 不 ...