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..." - ...
随机推荐
- 4,list,list的列表嵌套,range
list 索引,切片+步长 # li = [, True, (, , , , , , '小明',], {'name':'alex'}] #索引,切片,步长 # print(li[]) # print( ...
- redux学习总结
redux学习总结 *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !imp ...
- 学习笔记6——插件 API,“过滤器”(Filters)和“动作”(Actions)
WordPress 中有一种叫执行挂勾的机制,允许插件把一些功能“挂载”到 WordPress 当中.也就是说,在系统运行至某一个环节时,去调用插件内的一些函数.执行挂勾分为两种: 动作 (Actio ...
- 九度oj 题目1173:查找
题目描述: 输入数组长度 n 输入数组 a[1...n] 输入查找个数m 输入查找数字b[1...m] 输出 YES or NO 查找有则YES 否则NO . 输入: 输入有多组数据. ...
- 跟着xiaoxin巨巨做cf
cf 385 C. Bear and Prime Numbers 题目大意:有一个数列{xi},每次给出一个询问[l, r],即问 S(l ,r)是l和r之间的素数,f(p)表示数列{xi}中整除p的 ...
- Codeforces787D - Legacy
Description \(n(n\leq10^5)\)个点构成的有向图,有\(m(m\leq10^5)\)条连通信息,信息有三种: 1 u v w,表示存在一条边权为\(w\)的有向边\((u,v) ...
- 【单调队列+二分查找】bzoj 1012: [JSOI2008]最大数maxnumber
[题意] 维护一个单调递减的q数组,用id数组记录q数组的每个下标对应在原数组的位置,那么id数组一定有单调性(q数组中越靠后,原数组中也靠后),然后二分查找这个数 [AC] #include< ...
- Spoj-NETADMIN Smart Network Administrator
The citizens of a small village are tired of being the only inhabitants around without a connection ...
- uva 11916 解模方程a^x=b (mod n)
Emoogle Grid You have to color an M x N ( 1M, N108) two dimensional grid. You will be provided K ...
- 【网摘】sql 语句修改字段名称以及字段类型
网上摘抄,备份使用: 修改字段名: 下例将表 customers 中的列 contact title 重命名为 title. EXEC sp_rename 'customers.[contact ti ...