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..." - ...
随机推荐
- 学习笔记6——插件 API,“过滤器”(Filters)和“动作”(Actions)
WordPress 中有一种叫执行挂勾的机制,允许插件把一些功能“挂载”到 WordPress 当中.也就是说,在系统运行至某一个环节时,去调用插件内的一些函数.执行挂勾分为两种: 动作 (Actio ...
- 每天一个linux命令目录(转)
一. 文件目录操作命令: 1.每天一个linux命令(1):ls命令 2.每天一个linux命令(2):cd命令 3.每天一个linux命令(3):pwd命令 4.每天一个linux命令(4):mk ...
- 性能学习之--loaderunner中run-time setting常用功能
先打开run-time setting界面 一.Run Logic 设置迭代次数,只控制action的迭代次数,init和end只执行一次. 如果迭代次数设置10,10个并发用户,那么init和e ...
- 【Luogu】P1602Sramoc问题(堆)
题目链接 很巧妙的想法.一开始将1~k-1加入堆中,然后每次从堆里取出一个最小的,判断是不是答案,如果不是,那么就枚举新数的末一位加上. 代码如下 #include<cstdio> #in ...
- SPOJ GSS7 Can you answer these queries VII ——树链剖分 线段树
[题目分析] 问题放到了树上,直接链剖+线段树搞一搞. 调了300行+. (还是码力不够) [代码] #include <cstdio> #include <cstring> ...
- [POJ1664] 放苹果 (动态规划,组合数学)
题目描述 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分发(5,1,1和1,1,5是同一种方法) 输入输出格式 输入格式: 第一行是测试数据的数目t(0 <= ...
- uva 11235 RMQ范围最大值
题目大意:给一个整数上升序列,对于一系列询问区间(i,j),回答这段区间出现次数最多值所出现的次数. 分析:一个上升序列,相同的值聚集在一起,把相同的值的区间看作一个整体,假设这样的整体有n个,把他们 ...
- 【SPOJ687&POJ3693】Maximum repetition substring(后缀数组)
题意: n<=1e5 思路: From http://hzwer.com/6152.html 往后匹配多远 r 用ST表求lcp即可...往前 l 就把串反过来再做一下.. 但是有可能求出来的最 ...
- gridview无数据源实现更新数据库(即断开更新数据库)
原文发布时间为:2008-08-01 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- Android网络编程之HttpClient运用
Android网络编程之HttpClient运用 在 Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来方便我们 ...