[POJ 2184]--Cow Exhibition(0-1背包变形)
题目链接:http://poj.org/problem?id=2184
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9479 | Accepted: 3653 | 
Description
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
Sample Input
5
-5 7
8 -6
6 -3
2 1
-8 -5
Sample Output
8 题目大意:
N头奶牛中(N大于0且N小于100) 选择一部分去参加一个展览。 每头奶牛有两个指标,Si和Fi(-1000<=Si,Fi<=1000),
分别代表每头奶牛的聪明指数和快乐指数。求所挑选奶牛的Si和Fi的总和最大值,且Si和Fi各自的和数不能小于0。 解题思路:怎么说呐~~此题略坑,也是看了不少博客a出来的,巧妙的运用dp,吧si的正负影响转移了,
然后0-1背包的思路来做按,依照si正负按照正反两个方向dp,然后在满足条件的情况下,
然后你会发现最后i的增量就是满足条件的si的和,然后遍历dp数组筛选即可~~(具体的看看代码吧) 代码如下:
#include<iostream>
#include<cstring>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = ;
const int add = ;
int dp[], si, fi, n, i, j, ans;
int main()
{
cin >> n;
memset(dp, -inf, sizeof(dp));
dp[add] = ;
for (i = ; i <= n; i++)
{
cin >> si >> fi;
if (si > )
{
for (j = maxn + add; j >= si; j--)
if (dp[j - si] + fi > dp[j] && dp[j - si] > -inf)//注意边界判断
dp[j] = dp[j - si] + fi;
}
else
{
for (j = ; j <= maxn + add + si; j++)
if (dp[j - si] + fi > dp[j] && dp[j - si] > -inf)
dp[j] = dp[j - si] + fi;
}
}
for (i = add; i <= maxn + add; i++)
if (dp[i] >= && i + dp[i] - add > ans)
ans = i + dp[i] - add;
cout << ans << endl;
return ;
}
[POJ 2184]--Cow Exhibition(0-1背包变形)的更多相关文章
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
		
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
 - poj 2184 Cow Exhibition(01背包)
		
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10882 Accepted: 4309 D ...
 - POJ 2184 Cow Exhibition (01背包变形)(或者搜索)
		
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10342 Accepted: 4048 D ...
 - poj 2184 Cow Exhibition(dp之01背包变形)
		
Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...
 - POJ 2184:Cow Exhibition(01背包变形)
		
题意:有n个奶牛,每个奶牛有一个smart值和一个fun值,可能为正也可能为负,要求选出n只奶牛使他们smart值的和s与fun值得和f都非负,且s+f值要求最大. 分析: 一道很好的背包DP题,我们 ...
 - POJ 2184 Cow Exhibition (01背包的变形)
		
本文转载,出处:http://www.cnblogs.com/Findxiaoxun/articles/3398075.html 很巧妙的01背包升级.看完题目以后很明显有背包的感觉,然后就往背包上靠 ...
 - poj 2184 Cow Exhibition(背包变形)
		
这道题目和抢银行那个题目有点儿像,同样涉及到包和物品的转换. 我们将奶牛的两种属性中的一种当作价值,另一种当作花费.把总的价值当作包.然后对于每一头奶牛进行一次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] ...
 
随机推荐
- HDU3535-AreYouBusy
			
描述: As having become a junior, xiaoA recognizes that there is not much time for her to AC problems, ...
 - 10,随机等概率的输出m个不重复的数
			
今天看到一段代码,可以从0.....n-1中随机等概率的输出m个不重复的数(n远远大于m).遂记录下来. 首先,产生随机数,不免要用到srand,rand函数.先简单介绍下两个函数. 1,void s ...
 - MSSQL SERVER 2008 R2 无法连接到数据库,用户sa登录失败,错误:18456
			
原因:勾选了强制实施密码策略,但是设置的密码很简单依然可以,比如:123456 这是为什么?原来,这个功能要用到NetValidatePasswordPolicy() API这个函数. (该功能只有在 ...
 - utf8_general_ci 、utf8_general_cs和utf8_bin的区别
			
用了这么长时间,发现自己竟然不知道utf_bin和utf_general_ci这两者到底有什么区别..ci是 case insensitive, 即 "大小写不敏感", a 和 A ...
 - Windows Azure 网站上的 WordPress 3.8
			
 编辑人员注释:本文章由 Windows Azure 网站团队的项目经理 Sunitha Muthukrishna 和 Windows Azure 网站开发人员体验合作伙伴共同撰写. WordPr ...
 - poj 3335 Rotating Scoreboard - 半平面交
			
/* poj 3335 Rotating Scoreboard - 半平面交 点是顺时针给出的 */ #include <stdio.h> #include<math.h> c ...
 - uva311 - Packets(贪心)
			
题目:311 - Packets 题目大意:给出1*1, 2*2,3 *3, 4*4, 5*5, 6*6的箱子的个数,如今有若干个6*6的箱子,问最少用多少个箱子能够将给定的箱子都装进去. 解题思路: ...
 - VCS引起的oracle数据库异常重新启动一例
			
1. 环境描写叙述 操作系统版本号:SUSE Linux Enterprise Server 10 sp2 (x86_64) 数据库版本号:Oracle 11.1.0.7.16 VCS版本号:5.1 ...
 - BarChart控件的使用
			
The HTML Markup <asp:BarChart ID=" ChartType="Column" ChartTitle="United Stat ...
 - [译]Stairway to Integration Services Level 8 - SSIS 工作流管理高级
			
介绍 在前两个章节我们,建立了一个新的SSIS包,简单的使用了一下scripting还有优先约束,并且测试了MaxConcurrentExecutables 属性. 同时实验了 “On Succe ...