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\) 种物品是否被选. 为保证从小到大和字典序,先将瓶子按大小排序 ...
随机推荐
- JDBC——数据层DAO
DAO:Data Access Object DAO 数据层 Service 逻辑业务层 View 视图层 entity 实体层 实现增.删.改.查的数据层 public class EmpDA ...
- JavaScript之数据类型
1. 种类 5种基本类型:Number.String.Null.Undefined.Boolean 1种对象类型:Object(Function.Array.Date) 特别注意:当把基本类型尝试以对 ...
- ios 相册相关
1.ALAssetsLibrary 系统中的资源库,可以使用他来访问资源库中的资源,照片.视屏等. [ALAssetsLibrary authorizationStatus];获取当前应用能否 ...
- c#泛型方法重载
这里存在普通的方法Foo和泛型方法Foo,如果直接调用: 则会自动优先匹配对应的非泛型方法.输出如下: 但需要注意的是,这一匹配过程是在编译过程进行的,所以如果是通过其它泛型间接调用.则只会调用对应的 ...
- 注释玩转webapi
using System; using System.Collections.Generic; using System.Net.Http.Formatting; using System.Web.H ...
- mongodb write 【摘自网上,只为记录,学习】
mongodb有一个write concern的设置,作用是保障write operation的可靠性.一般是在client driver里设置的,和db.getLastError()方法关系很大 一 ...
- 实例:图像载入、显示、混合与输出[OpenCV 笔记8]
是的是的,忍着尿意努力更新,就是为了更到wuli男神的部分,当然要把男神放在前面镇楼,欢迎下载配图,具体操作见code wuliEddie.jpg logo.png results.jpg LoadS ...
- Android版多线程下载器核心代码分享
首先给大家分享多线程下载核心类: package com.example.urltest; import java.io.IOException; import java.io.InputStream ...
- OpenJudge 2815 城堡问题 / Poj 1164 The Castle
1.链接地址: http://bailian.openjudge.cn/practice/2815/ http://poj.org/problem?id=1164 2.题目: 总时间限制: 1000m ...
- webstorm快捷方式
刚开始在使用webstrom的时候,不知道快捷方式,感觉自己把webstorm当做记事本使用,真的挺傻的,在朋友的指导下,原来webstorm有快捷方式 一.界面操作 快捷键 说明 ctrl+shif ...