POJ 2184 Cow Exhibition (01背包变形)(或者搜索)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10342 | Accepted: 4048 |
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
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
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<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
#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背包变形)(或者搜索)的更多相关文章
- [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 (01背包的变形)
本文转载,出处:http://www.cnblogs.com/Findxiaoxun/articles/3398075.html 很巧妙的01背包升级.看完题目以后很明显有背包的感觉,然后就往背包上靠 ...
- 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] ...
- POJ-2184 Cow Exhibition(01背包变形)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10949 Accepted: 4344 Descr ...
- PKU 2184 Cow Exhibition 01背包
题意: 有一些牛,每头牛有一个Si值,一个Fi值,选出一些牛,使得max( sum(Si+Fi) ) 并且 sum(Si)>=0, sum(Fi)>=0 思路: 随便选一维做容量(比如Fi ...
- POJ 2184 Cow Exhibition(背包)
希望Total Smart和Totol Funess都尽量大,两者之间的关系是鱼和熊掌.这种矛盾和背包的容量和价值相似. dp[第i只牛][j = 当前TotS] = 最大的TotF. dp[i][j ...
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
- poj 2184 Cow Exhibition(dp之01背包变形)
Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...
随机推荐
- 【MySQL】可重复读模式下 unique key失效案例
一 [背景] 今天上午文能提笔安天下,武能上马定乾坤的登博给团队出了一道题目,谁先复现问题,奖励星巴克一杯.激起了一群忙碌的屌丝DBA的极大热情.问题是这样滴,如下图登博提示了几个细节: 1. ...
- MongoDB教程(笔记)
一.NoSQL简介 1.什么是NoSQL NoSQL,指的是非关系型的数据库.NoSQL有时也称作Not Only SQL的缩写,是对不同于传统的关系型数据库的数据库管理系统的统称. NoSQL用于超 ...
- selenium 切换窗口的几种方法
第一种方法: 使用场景: 打开多个窗口,需要定位到新打开的窗口 使用方法: # 获取打开的多个窗口句柄 windows = driver.window_handles # 切换到当前最新打开的窗口 d ...
- 【工具】Homebrew的安装及使用
Homebrew官网:http://brew.sh/index_zh-cn.html Homebrew是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件,相当于linux下的a ...
- 九度oj 题目1160:放苹果
题目描述: 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. 输入: 第一行是测试数据的数目t(0 <= t ...
- sql自动审核工具-inception
[inception使用规范及说明文档](http://mysql-inception.github.io/inception-document/usage/)[代码仓库](https://githu ...
- BZOJ 2244 [SDOI2011]拦截导弹 ——CDQ分治
三维偏序,直接CDQ硬上. 正反两次CDQ统计结尾的方案数,最后统计即可. #include <cstdio> #include <cstring> #include < ...
- 转载:shell中awk printf的用法
转载:http://www.linuxawk.com/jiaocheng/83.html 6. printf函数 打印输出时,可能需要指定字段间的空格数,从而把列排整齐.在print函数中使用制表 ...
- 小Z的袜子(bzoj 2038)
Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...
- JSP表单提交中文乱码
简要笔记:由于jsp默认表单提交编码方式是:ISO-8859-1,而我们需要的是utf-8或者是gbk码,故需要转化. 具体方法是:在表单处理文件中,将获取到的变量进行转换. String userN ...