"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. one or more listeners failed to start问题解决思路

    今日搭建一个web应用的时候总是遇到tomcat报错:one or more listeners failed to start. Full detail balabale....而且还没有其他提示, ...

  2. Python 学习笔记(十)Python集合(三)

    集合运算 元素与集合的关系 元素与集合的关系 ,就是判断某个元素是否是集合的一员."a" in aset >>> s =set([1,2,3,4]) >&g ...

  3. Servlet过滤器Filter和监听器

    一.Servlet过滤器的概念: *********************************************************************************** ...

  4. plsql误删除数据,提交事务后如何找回?

    select * from tbs_rep_template as of timestamp to_timestamp('2018-07-12 14:23:00', 'yyyy-mm-dd hh24: ...

  5. 自动曝光修复算法 附完整C代码

    众所周知, 图像方面的3A算法有: AF自动对焦(Automatic Focus)自动对焦即调节摄像头焦距自动得到清晰的图像的过程 AE自动曝光(Automatic Exposure)自动曝光的是为了 ...

  6. 关于windows下安装mysql数据库出现中文乱码的问题

    首先需要在自己安装的mysql路径下新建一个my.ini文件,如下: 然后在my.ini文件中输入一下内容,主要控制编码问题的为红框部分,如下: 为了方便大家使用,可以复制以下代码: [WinMySQ ...

  7. master.HMaster: Failed to become active master

    Hbase集群启动后自动退出,日志错误: fatal error: master.HMaster: Failed to become active master java.io.IOException ...

  8. Leecode刷题之旅-C语言/python-344反转字符串

    /* * @lc app=leetcode.cn id=344 lang=c * * [344] 反转字符串 * * https://leetcode-cn.com/problems/reverse- ...

  9. 数组循环左移 i 位

    数组左移 i 位 3 种方法 1.临时数组存储 先将前 i 个元素用数组存起来 再将后 n - i 个元素左移 i 位 最后将存起来的数组添加到后面去即可 2.通过多次调用左移 1 位的函数 3.翻转 ...

  10. [POJ1014]Dividing(二进制优化多重背包)

    #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int ...