POJ 2184 Cow Exhabition
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.
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int inf = <<;
const int dir = ;
int dp[];
struct s{
int s, f;
}arr[]; int main(){
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d%d",&arr[i].s,&arr[i].f); for(int i=;i<=;i++)
dp[i] = -inf;
dp[] = ; for(int i=;i<n;i++){
if(arr[i].s < && arr[i].f < )
continue;
if(arr[i].s>){
for(int j=;j>=arr[i].s;j--){
if(dp[j-arr[i].s] > -inf){
dp[j] = max(dp[j],dp[j-arr[i].s]+arr[i].f);
}
} }else {// arr[i].s < 0 arr[i].f>=0
for(int j=arr[i].s;j<=+arr[i].s;j++){
if(dp[j-arr[i].s] > -inf){
dp[j] = max(dp[j],dp[j-arr[i].s]+arr[i].f);
}
}
}
}
int ans = ;
for(int i=;i<=;i++){
if(dp[i]>=)
ans = max(ans, dp[i]+i-);
}
printf("%d\n",ans);
return ;
}
对代码进行了优化。
首先是将dp从数组名变成了指针,令dp指向buf[100000]
这样、数组下标为负数时也不会越界。
优化关键在设置了两个变量,分别记录了有效区间的两个端点。
比如说 在处理第一个数的时候整个区间的都是无法达到的(除了原点)
而再像优化前的代码一样遍历一个200000的区间是无用的冗余。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int inf = <<;
int buf[];
int *const dp = buf + ; int main(){
int n, s, f;
scanf("%d",&n); for(int i=;i<=;i++)
buf[i] = -inf;
dp[] = ;// dp[0] == buf[100000] int l = , r = ;// 有效区间的左端点和右端点
for(int i=;i<n;i++){
scanf("%d%d",&s,&f);
if(s< && f <)//舍弃无用点
continue;
if(s > ){// 体积为正数 所以从大到小dp
for(int i=r;i>=l;i--)
dp[i+s] = max(dp[i+s],dp[i]+f);
}else {
for(int i=l;i<=r;i++)
dp[i+s] = max(dp[i+s],dp[i]+f);
}
if(s>)
r += s;
else
l += s;
}
int ans = ;
for(int i=;i<=r;i++){
if(dp[i] >= )
ans = max(ans, dp[i]+i);
}
printf("%d\n",ans);
return ;
}
POJ 2184 Cow Exhabition的更多相关文章
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
- [POJ 2184]--Cow Exhibition(0-1背包变形)
题目链接:http://poj.org/problem?id=2184 Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total S ...
- poj 2184 Cow Exhibition(dp之01背包变形)
Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...
- POJ 2184 Cow Exhibition (01背包变形)(或者搜索)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10342 Accepted: 4048 D ...
- poj 2184 Cow Exhibition(01背包)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10882 Accepted: 4309 D ...
- POJ 2184 Cow Exhibition (01背包的变形)
本文转载,出处:http://www.cnblogs.com/Findxiaoxun/articles/3398075.html 很巧妙的01背包升级.看完题目以后很明显有背包的感觉,然后就往背包上靠 ...
- poj 2184 Cow Exhibition
// 给定n头牛,每头有属性智商和幽默感,这两个属性值有正有负,现在要从这n头牛中选出若干头使得他们的智商和与幽默感和不为负数,// 并且两者两家和最大,如果无解输出0,n<=100,-1000 ...
- poj 2184 Cow Exhibition(背包变形)
这道题目和抢银行那个题目有点儿像,同样涉及到包和物品的转换. 我们将奶牛的两种属性中的一种当作价值,另一种当作花费.把总的价值当作包.然后对于每一头奶牛进行一次01背包的筛选操作就行了. 需要特别注意 ...
- POJ 2184 Cow Exhibition 01背包
题意就是给出n对数 每对xi, yi 的值范围是-1000到1000 然后让你从中取若干对 使得sum(x[k]+y[k]) 最大并且非负 且 sum(x[k]) >= 0 sum(y[k] ...
随机推荐
- springboot自定义异常页面
废话不多,直接开始. 项目目录: 说明:springboot 静态文件放在static目录中,如images中放的图片:templates目录下error中存放的是错误页面,如500.html代表50 ...
- 使用PIE.htc让万恶的IE内核浏览器IE6\7\8支持CSS3部分属性
万恶的IE内核浏览器,这是多少前端程序员头疼的事情... 今天给大家介绍一下如何用 PIE.htc 来让IE浏览器支持CSS3的 border-radius.box-shadow.CSS3 Backg ...
- Reading Notes : 180215 计算机系统
读书<计算机组成原理>,<鸟哥的Linux私房菜 基础篇>百度百科,内容摘自<计算机组成原理>,<鸟哥的Linux私房菜 基础篇> 计算机系统 在前面几 ...
- 解决ssh连接linux系统特别慢的问题
新安装的centos系统,发现ssh连接很慢,因为是测试环境,对安全的要求不高,所以完全可以更快的连接,下面一起来解决这个问题. 一.分析主要原因: 1.SSH的反向DNS解析会消耗大量时间 2.GS ...
- 笔记: js构造函数与原型
目录 构造函数与原型介绍 涉及三种引用的操作 有关原型及原型链的一些相关方法总结 @ 构造函数与原型介绍 1.函数与函数的原型对象(prototype object): 在JavaScript中,创建 ...
- obfuscator-llvm Theos 集成配置
之前我写过一篇文章是关于在 Xcode 里怎么集成配置 obfuscator-llvmobfuscator-llvm Xcode集成配置 有些情况下我们使用 Theos 开发 tweak,需要将 ob ...
- 跨域详解之-----Jsonp跨域
一.通过jsonp跨域 在js中,我们直接用XMLHttpRequest请求不同域上的数据时,是不可以的.但是,在页面上引入不同域上的js脚本文件却是可以的,jsonp正是利用这个特性来实现的. 比如 ...
- Yii2 yiisoft/mongodb 手动安装
手动将yiisoft/mongodb下载到vendor/yiisoft目录(注意约束条件). 在vendor/yiisoft/extensions.php 中添加 'yiisoft/yii2-mong ...
- For-each Loop,Index++ Loop , Iterator 那个效率更高
平时在写Java/C# 程序的时候,会写很多的Loop 语句,for() 及 Iterator loop 及Java 8 的foreach Loop, 这些Loop 那种效率最高呢?写个小程序测试一下 ...
- MapReduce输入输出的处理流程及combiner
MapReduce 的输入输出 MapReduce 框架运转在<key,value> 键值对上,也就是说,框架把作业的输入看成是一组<key,value>键值对,同样也产生一组 ...