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..." - ...
随机推荐
- 转:Ubuntu下ibus-sunpinyin的安装及翻页快捷键设置!
在windows下,好多人都已经习惯了使用搜狗拼音,到ubuntu下,忽然没有极为顺手的输入法,实为郁闷,但是确实还没有for linux版本的搜狗使用,这是搜狗的商业策略,我们无法掌控,但是,如果你 ...
- linux下防火墙iptables原理及使用
iptables简介 netfilter/iptables(简称为iptables)组成Linux平台下的包过滤防火墙,与大多数的Linux软件一样,这个包过滤防火墙是免费的,它可以代替昂贵的商业防火 ...
- php 上传文件名出现乱码
想必很多朋友在进行utf8编码的php开发上传功能的时候,都会遇到这样的一个问题,就是上传中文文件名的文件时,文件名会变成乱码,其实我们可以用iconv函数对文件名进行重新编码就解决问题了 可能会有不 ...
- 【转】OPC远程访问相关配置信息
原文:http://blog.gkong.com/kking_25653.ashx 对于远程访问OPC服务器,需要在客户和服务器计算机上都进行DCOM设置,本文提供一些具体配置方法.(by Kevin ...
- 九度oj 题目1137:浮点数加法
题目描述: 求2个浮点数相加的和 题目中输入输出中出现浮点数都有如下的形式:P1P2...Pi.Q1Q2...Qj对于整数部分,P1P2...Pi是一个非负整数 对于小数部分,Qj不等于0 输入: 对 ...
- Android自制rom,为update.zip签名
确认已经安装好openssl openssl genrsa -out key.pem openssl req -new -key key.pem -out request.pem openssl x5 ...
- sublime text2-text3 定义的不同浏览器的预览快捷键
sublime text3 自己定义的不同浏览器的预览快捷键突然全部失效了,搞到现在一直没闹清楚怎么回事,翻看插件发现SideBarEnhancements这插件刚更新了,快捷键也是依赖这个插件弄得. ...
- Ubuntu Flask安装与配置(待整理)
工作中开发需要用到python的flask框架,无奈网络上的资源很少,连基本的安装和配置都不全,在这做一下整理,方便以后用到. ———————————————————————————— 由于比较繁琐, ...
- 洛谷P1447 - [NOI2010]能量采集
Portal Description 给出\(n,m(n,m\leq10^5),\)计算\[ \sum_{i=1}^n \sum_{j=1}^m (2gcd(i,j)-1)\] Solution 简单 ...
- cf468B Two Sets
Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A an ...