POJ2184 Cow Exhibition[DP 状态负值]
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12420 | Accepted: 4964 |
Description
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
* Lines 2..N+1: Two space-separated integers Si and Fi, respectively the smartness and funness for each cow.
Output
Sample Input
5
-5 7
8 -6
6 -3
2 1
-8 -5
Sample Output
8
Hint
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
#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 状态负值]的更多相关文章
- POJ-2184 Cow Exhibition(01背包变形)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10949 Accepted: 4344 Descr ...
- poj2184 Cow Exhibition(p-01背包的灵活运用)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=2184">http://poj.org/problem?id=2184 Descrip ...
- poj2184 Cow Exhibition【01背包】+【负数处理】+(求两个变量的和最大)
题目链接:https://vjudge.net/contest/103424#problem/G 题目大意: 给出N头牛,每头牛都有智力值和幽默感,然后,这个题目最奇葩的地方是,它们居然可以是负数!! ...
- POJ 2184 Cow Exhibition (带负值的01背包)
题意:给你N(N<=100)只牛,每只牛有一个智慧值Si和一个活泼值Fi,现在要从中找出一些来,使得这些牛智慧值总和S与活泼值总和F之和最大,且F和S均为正.Si和Fi范围在-1000到1000 ...
- poj2184 Cow Exhibition
思路: dp+滚动数组. 类似01背包. 实现: #include <iostream> #include <cstdio> #include <algorithm> ...
- 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 ...
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
- poj 2184 Cow Exhibition(01背包)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10882 Accepted: 4309 D ...
- POJ 2184 Cow Exhibition (01背包变形)(或者搜索)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10342 Accepted: 4048 D ...
随机推荐
- 用TypeScript开发了一个网页游戏引擎,开放源代码
最开始学习电脑编程的原动力之一就是想自己编写游戏,一方面很好奇这些游戏是怎么做出来的,另一方面觉得有些地方设计的不合理,希望电脑游戏既能让人玩的有趣,又不浪费时间. 学校五年,毕业十年,学用了十多种编 ...
- [Android]实现类似微信的延迟加载的Fragment——LazyFragment
以下内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4303910.html 参考微信,使用ViewPager来显示不同的t ...
- Android 短视频拍摄、拍照滤镜 第三方库SDK
视频 1.趣拍云服务 http://vcs.qupai.me/ 拍照 1.camera360 SDk 拍照滤镜 http://www.camera360.com/ 2 .凃图 http://tusdk ...
- tomcat WEB-INF中的结构
tomcat中 WEB-INF中结构包含3个东西:web.xml,classes文件夹,lib文件夹 web.xml用来配置web中服务调用的uri和对应服务指定的是哪个class文件 classes ...
- 【代码笔记】iOS-离线地图
一,效果图. 二,工程图. 三,代码. ViewController.h #import <UIKit/UIKit.h> #import <CoreLocation/CoreLoca ...
- 设计模式 之 装饰者(Decorator)模式
装饰者模式(Decorator):动态地为一个对象添加一些额外的职责,若要扩展一个对象的功能,装饰者提供了比继承更有弹性的替代方案. 结构图: 抽象构件类(Component):给出一个抽象的接口,用 ...
- XMLA ODBO 以及OLAP服务提供者自定义的协议,我们如何选择
参考 SAP给他的客户的帮助<ODBO, BAPI and XMLA - Sap> SAP BW 提供的查询接口: 接口 查询语言 调用接口 OS平台 客户端开发 ODBO MDX C ...
- IT软件开发常用英语词汇
Aabstract 抽象的abstract base class (ABC)抽象基类abstract class 抽象类abstraction 抽象.抽象物.抽象性access 存取.访问access ...
- WPF学习之路(二) XAML(续)
属性 简单属性 前面用到的Width/Height都是简单属性,其赋值一定要放到双引号里 XAML解析器会根据属性的类型执行隐式转换 与C#的区别 SolidBrush.Color = Colors. ...
- yii2 GridView常见操作
作者:白狼 出处:http://www.manks.top/article/yii2_gridview 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则 ...