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

 
显然是个0/1背包 把 s 当做体积,f 当做重量就可以。
 但是问题在于物品的体积有负数。
一般情况下,我们做的背包都是体积在一个以 0 为左端点的区间。
这道题变成了 一个 -1000*100 到 1000*100 的区间,我们可以将这个区间整体向右平移 100000
这样的话 原点移动到了 100000 这个点。左端点从 -1000*100 变成了 0
然后再分类讨论 s 的正负,进而决定是从大到小还是从小到大dp
 
#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的更多相关文章

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

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

  2. [POJ 2184]--Cow Exhibition(0-1背包变形)

    题目链接:http://poj.org/problem?id=2184 Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  3. poj 2184 Cow Exhibition(dp之01背包变形)

    Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...

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

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

  5. poj 2184 Cow Exhibition(01背包)

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

  6. POJ 2184 Cow Exhibition (01背包的变形)

    本文转载,出处:http://www.cnblogs.com/Findxiaoxun/articles/3398075.html 很巧妙的01背包升级.看完题目以后很明显有背包的感觉,然后就往背包上靠 ...

  7. poj 2184 Cow Exhibition

    // 给定n头牛,每头有属性智商和幽默感,这两个属性值有正有负,现在要从这n头牛中选出若干头使得他们的智商和与幽默感和不为负数,// 并且两者两家和最大,如果无解输出0,n<=100,-1000 ...

  8. poj 2184 Cow Exhibition(背包变形)

    这道题目和抢银行那个题目有点儿像,同样涉及到包和物品的转换. 我们将奶牛的两种属性中的一种当作价值,另一种当作花费.把总的价值当作包.然后对于每一头奶牛进行一次01背包的筛选操作就行了. 需要特别注意 ...

  9. POJ 2184 Cow Exhibition 01背包

    题意就是给出n对数 每对xi, yi 的值范围是-1000到1000 然后让你从中取若干对 使得sum(x[k]+y[k]) 最大并且非负   且 sum(x[k]) >= 0 sum(y[k] ...

随机推荐

  1. 【Django笔记四】Django2.0中的表单

    一.环境版本信息: 操作系统:windows10 Django版本:2.0.5 Python版本:3.6.4 Mysql版本: 5.5.53   安装mysql 二.基础信息 1.App中的模型mod ...

  2. Spring Boot 微信-验证服务器有效性【转】

    转:https://blog.csdn.net/jeikerxiao/article/details/68064145 概述 接入微信公众平台开发,开发者需要按照如下步骤完成: 在自己服务器上,开发验 ...

  3. python函数基本介绍

    函数 1.函数结构 def 是函数的定义关键字,my_len是函数名.()传参用,冒号下面都是函数体. 执行函数方法:函数名加括号来执行函数.My_len() 举例: # s = 'lkfjsjulk ...

  4. 浅谈HTML5中canvas中的beginPath()和closePath()的重要性

    beginPath的作用很简单,就是开始一段新的路径,但在使用canvas绘图的过程中却非常重要 先来看一小段代码: var ctx=document.getElementById("can ...

  5. 06 day小数据池

    1.小数据池,id() 小数据池针对的是: int ,str,bool-----都是不可变的数据类型  a.int 类型 a = 1000 b = 1000 print(id(a), id(b))   ...

  6. Currency Helper

    using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; /// <summary> /// 货币 ...

  7. EFI分区删除的有效方法

    用Diskpart命令,可以方便的删除EFI系统分区. 一,win + R, 输入cmd,回车. 二,输入 Diskpart ,回车,得到 三,再输入 list disk , 回车,查看磁盘信息 四, ...

  8. rails中发送ajax请求

    最近在写一个blog系统练练手,遇到一个一个问题,用户添加评论的时候想发送ajax请求,但是rails里的ajax和Python中的不太一样,Python中的ajax是用js,jquery实现的和ra ...

  9. java入门---基本数据类型之内置数据类型

        变量就是申请内存来存储值.也就是说,当创建变量的时候,需要在内存中申请空间.内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来储存该类型数据.     因此,通过定义不同类型的变 ...

  10. HTML5 tricks for mobile

    iOS 7.1的Safari为meta标签新增minimal-ui属性,在网页加载时隐藏地址栏与导航栏 01. Creating a fullscreen experience On Android  ...