POJ_2184_Cow_Exhibition_(动态规划,背包)
描述
http://poj.org/problem?id=2184
n只奶牛,每只都有智商s_i和情商f_i,取出若干只,保证智商之和与情商之和都不为负的情况下,让两者之和最大.
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 11635 | Accepted: 4610 |
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
分析
是一个关于取与不取得问题,而当智商之和一定时,情商之和越大越好,所以类似01背包,用智商之和作为dp数组下标,dp[i]表示智商之和为i时,情商的最大值,动规结束后扫一遍,找到符合要求的最有答案即可.
但是注意这里用智商之和作为下标时,智商之和有可能为负,所以用idx把整个数组向右移.统计智商之和最小的值,向右移动这个值即可.则dp[i]表示的是智商之和为(i-idx)的情商最大值.
注意:
1.开始时dp数组要全部赋为赋值.
2.赋的值不能使-0x7fffffff,因为可能会和负的情商相加...(貌似不是第一次犯这种错误了0.0)
#include <cstdio>
#include <algorithm>
#define for1(i,a,n) for(int i=(a);i<=(n);i++)
#define read(a) a=getnum()
using namespace std; const int INF=<<;
int n,mins,maxs;
int s[+],f[+];
int dp[**+]; inline int getnum(){int r=,k=;char c;for(c=getchar();c<''||c>'';c=getchar())if(c=='-')k=-;for(;c>=''&&c<='';c=getchar())r=r*+c-'';return r*k;} void solve()
{
int idx=mins;
int range=idx+maxs;
for1(i,,range) dp[i]=-INF;
dp[idx]=;
for1(i,,n)
{
if(s[i]>=)
{
for(int j=range;j>=s[i];j--)
{
dp[j]=max(dp[j],dp[j-s[i]]+f[i]);
}
}
else
{
for(int j=;j-s[i]<=range;j++)
{
dp[j]=max(dp[j],dp[j-s[i]]+f[i]);
}
}
}
int ans=-INF;
for(int i=idx;i<=range;i++)
{
if(dp[i]<) continue;
ans=max(ans,i-idx+dp[i]);
}
printf("%d\n",ans);
} void init()
{
read(n);
for1(i,,n)
{
read(s[i]);
read(f[i]);
if(s[i]>) maxs+=s[i];
else mins-=s[i];
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("cow.in","r",stdin);
freopen("cow.out","w",stdout);
#endif
init();
solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("cow.out");
#endif
return ;
}
POJ_2184_Cow_Exhibition_(动态规划,背包)的更多相关文章
- Bzoj 1042: [HAOI2008]硬币购物 容斥原理,动态规划,背包dp
1042: [HAOI2008]硬币购物 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1747 Solved: 1015[Submit][Stat ...
- Leetcode 494 Target Sum 动态规划 背包+滚动数据
这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20) ...
- hdu 3008:Warcraft(动态规划 背包)
Warcraft Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- POJ_2392_Space_Elevator_(动态规划,背包)
描述 http://poj.org/problem?id=2392 磊方块,每种方块有数量,高度,以及该种方块所能处在的最高高度.问最高磊多高? Space Elevator Time Limit: ...
- Contest1874 - noip基础知识五:动态规划(背包、树dp、记忆化、递推、区间、序列dp、dp优化)
传送门 T1 dp[n][m]=dp[n-1][m-1]+dp[n-m][m] T2 ans=cat(n)*(n!)2 卡特兰数 T3 dp[i][j]=sigma(dp[i-1][j-a[i ...
- BZOJ1222 [HNOI2001]产品加工 - 动态规划- 背包
题解 怎么看都不像是个背包,直到我看了题解→_→, 第一次碰到这么奇怪的背包= = 定一个滚动数组$F_i$, $i$表示机器$a$用了$i$的时间, $F_i$表示机器$b$用了$F_i$的时间, ...
- 【洛谷】【动态规划/背包】P1417 烹调方案
由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的食物准备填 ...
- 【洛谷】【动态规划/背包】P1833 樱花
[题目描述:] 爱与愁大神后院里种了n棵樱花树,每棵都有美学值Ci.爱与愁大神在每天上学前都会来赏花.爱与愁大神可是生物学霸,他懂得如何欣赏樱花:一种樱花树看一遍过,一种樱花树最多看Ai遍,一种樱花树 ...
- [USACO Section 5.3]量取牛奶 Milk Measuring (动态规划,背包$dp$)
题目链接 Solution 完全背包 \(dp\) , 同时再加一个数组 \(v[i][j]\) 记录当总和为\(j\) 时第 \(i\) 种物品是否被选. 为保证从小到大和字典序,先将瓶子按大小排序 ...
随机推荐
- ns2出现Client: Handoff Attempt的情况解决
找到mac/mac-802_11.cc,这是系统本身一个bug,对于adhoc网络无需进行切换尝试. > if (*rcount == 3 && handoff == 0) {& ...
- 注册dll
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, Syste ...
- swift-01-简述swift与OC区别
swift语言 Swift是Apple在WWDC2014所发布的一门编程语言,用来撰写OS X和iOS应用程序[1].在设计Swift时.就有意和Objective-C共存,Objective-C是A ...
- cocos2dx如何添加popScene的场景动画
说明 我们很容易在pushScene中添加动画 Director::getInstance()->pushScene(TransitionSlideInB::create(SCENE_TIME, ...
- VS中使用sqlite静态连接
说明 最近写的文章有些多,懒得打字了,就直接上文章.这里说明一下,我说的是VS,不是指定的哪一个VS版本.先下载官方的源文件:sqlite-amalgamation-3071502.zip 下载下来的 ...
- ScrollView 尽量避免嵌套RelativeLayout,非常惨痛的教训
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...
- Android MVP模式
转自http://segmentfault.com/blogs,转载请注明出处Android MVP Pattern Android MVP模式\[1\]也不是什么新鲜的东西了,我在自己的项目里也普遍 ...
- 配置php连接apache
配置php连接apache 1.安装php所需要的库 yum install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel li ...
- javascript 事件多次绑定和删除
同一个事件绑定多个事件处理程序(适合自己写)IE: 添加: 对象.attachEvent("on事件名","处理程序/函数名"); 执行顺序从后向前 删除: 对 ...
- JavaScript获取非行间样式/定义样式
html节点的样式分为以下几种 (1)浏览器默认样式 (2)引用样式(引用外部css文件的样式.style标签内定义的样式) 引用外部css样式:<link rel="styleshe ...