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 ...
随机推荐
- 新建WEB前端开发技术交流群---期待你的加入
这几天创建了一个前端开发的技术交流群,面向的对象主要包括正在学习前端开发的和即将步入前端开发行列的小伙伴儿,主要是为大家提供一个共同学习,共同进步的开放平台.无论你现在是在自学中,还是在培训中,甚至是 ...
- js基本算法:冒泡排序,二分查找
知识扩充: 时间复杂度:算法的时间复杂度是一个函数,描述了算法的运行时间.时间复杂度越低,效率越高. 自我理解:一个算法,运行了几次时间复杂度就为多少,如运行了n次,则时间复杂度为O(n). 1.冒泡 ...
- [javascript svg fill stroke stroke-width x y rect rx ry 属性讲解] svg fill stroke stroke-width rect 绘制具有圆角矩形属性讲解
<!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...
- Autodesk 最新开发技术研讨会 -8月22日-Autodesk北京办公室
为了增进与广大中国地区Autodesk产品的二次开发人员的了解与互动,帮助中国地区的Autodesk产品二次开发人员了解Autodesk最新的二次开发技术动向,并获得Autodesk公司专业开发支持顾 ...
- ZeroMq安装包的生成【ubuntu10】
生成方法添加源sudo add-apt-repository ppa:chris-lea/zeromqsudo add-apt-repository ppa:chris-lea/libpgmsudo ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q1-Q3)
这里我把从网上搜集到的针对Sharepoint 70-576的有关练习进行系统的解析,整理成一个系列, 分期.分批次共享出来,供大家研究. 70-573考试注重的是"知道"相关知识 ...
- 自定义有监听器的ScrollView
public class ObservableScrollView extends ScrollView { public ObservableScrollView(Context context) ...
- 将自己写的库上传到cocoapods(2015)
2015年以前上传到cocoapods的方式相较于现在比较麻烦,现在用不上在此也就不提了.现在上传到cocoapods只需要简单的几步即可. 1.首先你需要有一个自我感觉写的差不多的库. 2.注册tr ...
- C阶段【01】 - C基础
一.进制 进位方法:逢几进一(也就是几进制) 举例:十进制 12 : 二进制 0b(计算机前缀) 0b1011 : 八进制 0 073 :十六进制 0x 0xABCDEF 十进 ...
- 【问题排查】StringIndexOutOfBoundsException
工作中遇到 java.lang.StringIndexOutOfBoundsException ,查看网上资料,总结如下 1.异常定义: Java API指出StringIndexOutOfBound ...