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

Description

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

Source

 
两种做法:1 背包  2 DFS
 
 1 背包
1)把Smartness看成花费,把Funness看成价值。
2)因为有负数下标,所以要处理,最大是100*1000也就是100000.记作w所以开一个dp[2*w+5]的数组。w表示0,0到w-1和w+1到2w分别表示负的和正的1到100000.
3)状态转移方程:  dp[j]=max(dp[j],dp[j-s[i]]+f[i]); 但要注意01背包,在s[i]大于0时,从大到小的顺序,可以保证每件物品只取一次,在s[i]小于0时,要从小到大,才能保证每件物品只取一次. (s[i]<0时,j-s[i]>j)
4)最后遍历一下dp[i]+i-w最大的数即可。(i要从w开始!开始从0开始WA了一次)
#include<queue>
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define w 100000 int dp[w*+],s[],f[];
int n;
int main()
{
while(cin>>n)
{
for(int i=;i<=*w;i++)
dp[i]= -INF;
dp[w]=;
int sum=w;
for(int i=;i<=n;i++)
{
scanf("%d%d",&s[i],&f[i]);
if(s[i]>)sum+=s[i];
}
for(int i=;i<=n;i++)
{
if(s[i]< && f[i]<)
continue;
else if(s[i]>=)
{
for(int j=sum;j>=s[i];j--)
if(dp[j-s[i]]!= -INF)
dp[j]=max(dp[j],dp[j-s[i]]+f[i]);
}
else
{
for(int j=s[i];j<=sum+s[i];j++)
if(dp[j-s[i]]!= -INF)
dp[j]=max(dp[j],dp[j-s[i]]+f[i]);
}
}
int ans=-INF;
for(int i=w;i<=sum;i++)
if(dp[i]>=)
ans=max(ans,dp[i]+i-w); cout<<ans<<endl;
}
return ;
}

2 DFS

1)用DFS遍历所有可能,最后sumS+sumF大于之前ans且sumF,sumS均>=0则替换ans
2)最重要的是剪枝,开始数据处理如果s[i],f[i]均小于0,剔除数据,均大于0直接加到sumS和sumF中
3)将s[i]与f[i]的和排序,大的可能是优先会选的,然后用sum数组表示选到第i个时最大不会超过的数,如果这个数都小于之前ans,就不用再走了return(这个是最主要的剪枝)
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 105 int s[N],f[N],a[N],sf[N],n,df,ds,ans,sumS,sumF,sum[N]; void dfs(int x)
{
if(x==n+)
{
if(sumS+sumF>ans && sumS>= && sumF>=)
ans=sumS+sumF;
return ;
} if(sf[x]<= && sumS+sumF<ans)return;//剪枝(和小于0存在的唯一目的就让前面和比较大但是会出现两个属性中有负数的变成正数,然而如果和还不如之前的ans就没有存在的必要了
if(sumS+sumF+sum[x]<=ans)return; //剪枝(主要靠这个,见下) sumS+=s[x];
sumF+=f[x];
dfs(x+);
sumS-=s[x];
sumF-=f[x];
dfs(x+);
} int main()
{
while(~scanf("%d",&n))
{
sumS=sumF=;
for(int i=;i<=n;i++)
{
scanf("%d%d",&s[i],&f[i]);
if(s[i]< && f[i]<)
{
i--,n--;
continue;
}
if(s[i]>= && f[i]>=)
{
sumS+=s[i];
sumF+=f[i];
i--;n--;
continue;
}
sf[i]=(s[i]+f[i]);
} for(int i=;i<=n-;i++)//排序,这部分可以用结构体加sort写,目的让两个值都高的在前面
for(int j=i+;j<=n;j++)
{
if(sf[i]<sf[j])
{
swap(sf[i],sf[j]);
swap(s[i],s[j]);//这个不能忘了
swap(f[i],f[j]);//同上
}
} memset(sum,,sizeof(sum));
for(int i=n;i>=;i--)//sum[i]表示排序之后选到第i头牛时一个大致的数(比实际数还大)
if(sf[i]<=) sum[i]=;//所以如果sumS+sumF加sum[i]都不能大于之前的ans把,那这条路肯定是不行的就return
else sum[i]=sum[i+]+sf[i]; ans=sumS+sumF;
dfs();
printf("%d\n",ans);
}
return ;
}

POJ 2184 Cow Exhibition (01背包变形)(或者搜索)的更多相关文章

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

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

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

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

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

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

  4. POJ 2184 Cow Exhibition 01背包

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

  5. POJ-2184 Cow Exhibition(01背包变形)

    Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10949 Accepted: 4344 Descr ...

  6. PKU 2184 Cow Exhibition 01背包

    题意: 有一些牛,每头牛有一个Si值,一个Fi值,选出一些牛,使得max( sum(Si+Fi) ) 并且 sum(Si)>=0, sum(Fi)>=0 思路: 随便选一维做容量(比如Fi ...

  7. POJ 2184 Cow Exhibition(背包)

    希望Total Smart和Totol Funess都尽量大,两者之间的关系是鱼和熊掌.这种矛盾和背包的容量和价值相似. dp[第i只牛][j = 当前TotS] = 最大的TotF. dp[i][j ...

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

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

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

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

随机推荐

  1. grunt与requirejs结合使用

    // 多个js压缩成一个js // Project configuration. module.exports = function(grunt) { // 使用严格模式 'use strict'; ...

  2. luogu2216 [HAOI2007]理想的正方形

    先对于每一行中长度为 n 的列用单调队列搞出它们的最小/大值,再将这些长度为 n 的列想象成点再对行跑一遍 #include <iostream> #include <cstring ...

  3. 【java基础 10】hash算法冲突解决方法

    导读:今天看了java里面关于hashmap的相关源码(看了java6和java7),尤其是resize.transfer.put.get这几个方法,突然明白了,为什么我之前考数据结构死活考不过,就差 ...

  4. 【JavaScript 13—应用总结】:锁屏遮罩

    导读:上次说了,当弹出登录框时,由于背景色和弹出框时一样的,这样子,其实比较难聚焦到底该操作哪一块.所以,如果,有了颜色的区分,那么通过屏幕遮罩的效果,就可以将我们希望要被处理的东西突出显示.也就达到 ...

  5. 编辑被标记为“只读”的Word文档

    从邮件接收到的Word文档,打开时总是被标记为“只读”,在阅读时对其进行编辑,但不能保存,会提示文档为只读的.要想对其进行编辑并保存,需要进行一定的操作. 进入文件所在的目录,鼠标右键点击Word文档 ...

  6. 最短路POJ 1062 昂贵的聘礼

    C - 昂贵的聘礼 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit St ...

  7. HDU-4849 Wow! Such City!,最短路!

    Wow! Such City!    题意:题面很难理解,幸亏给出了提示,敲了一发板子过了.给出x数组y数组和z数组的求法,并给出x.y的前几项,然后直接利用所给条件构造出z数组再构造出C数组即可,C ...

  8. Maven部署异常:on project standalone-pom: Cannot deploy artifact from the local repository解决方法

    MAVEN部署异常 org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache. ...

  9. 【bzoj4826】[Hnoi2017]影魔 单调栈+可持久化线段树

    题目描述 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还有英雄.每一个灵魂,都有着自己 ...

  10. 关于错误Access Violation和too many consecutive exceptions 解决方法

    关于错误Access Violation和too many consecutive exceptions 解决方法 “如果DLL中用到了DELPHI的string类型,则DLL和主程序中都需要加上Sh ...