描述


http://poj.org/problem?id=2184

n只奶牛,每只都有智商s_i和情商f_i,取出若干只,保证智商之和与情商之和都不为负的情况下,让两者之和最大.

Cow Exhibition
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11635   Accepted: 4610

Description

"Fat and docile, big and dumb, they look so stupid, they aren't much
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

* Line 1: A single integer N, the number of cows

* Lines 2..N+1: Two space-separated integers Si and Fi, respectively the smartness and funness for each cow.

Output

* Line
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

OUTPUT DETAILS:

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_(动态规划,背包)的更多相关文章

  1. Bzoj 1042: [HAOI2008]硬币购物 容斥原理,动态规划,背包dp

    1042: [HAOI2008]硬币购物 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1747  Solved: 1015[Submit][Stat ...

  2. Leetcode 494 Target Sum 动态规划 背包+滚动数据

    这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20) ...

  3. hdu 3008:Warcraft(动态规划 背包)

    Warcraft Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  4. POJ_2392_Space_Elevator_(动态规划,背包)

    描述 http://poj.org/problem?id=2392 磊方块,每种方块有数量,高度,以及该种方块所能处在的最高高度.问最高磊多高? Space Elevator Time Limit: ...

  5. 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 ...

  6. BZOJ1222 [HNOI2001]产品加工 - 动态规划- 背包

    题解 怎么看都不像是个背包,直到我看了题解→_→, 第一次碰到这么奇怪的背包= = 定一个滚动数组$F_i$, $i$表示机器$a$用了$i$的时间, $F_i$表示机器$b$用了$F_i$的时间, ...

  7. 【洛谷】【动态规划/背包】P1417 烹调方案

    由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船飞到一半,gw发现了一个很严重的问题:肚子饿了~ gw还是会做饭的,于是拿出了储藏的食物准备填 ...

  8. 【洛谷】【动态规划/背包】P1833 樱花

    [题目描述:] 爱与愁大神后院里种了n棵樱花树,每棵都有美学值Ci.爱与愁大神在每天上学前都会来赏花.爱与愁大神可是生物学霸,他懂得如何欣赏樱花:一种樱花树看一遍过,一种樱花树最多看Ai遍,一种樱花树 ...

  9. [USACO Section 5.3]量取牛奶 Milk Measuring (动态规划,背包$dp$)

    题目链接 Solution 完全背包 \(dp\) , 同时再加一个数组 \(v[i][j]\) 记录当总和为\(j\) 时第 \(i\) 种物品是否被选. 为保证从小到大和字典序,先将瓶子按大小排序 ...

随机推荐

  1. 网易游戏QA工程师笔试回忆-2012.9【个人题解】

    ========================转帖======================== 网易游戏QA工程师笔试回忆-2012.9 刚刚从武大回来,趁热回忆下题目,给以后的XDJMs参考. ...

  2. OC与Swift的区别三(条件语句)

    11.swift中的switch结构 区别一: oc中switch条件只可以放整数 swift中switch条件可以放几乎任何数据类型 区别二: oc中每一个case中应有break,如果没有brea ...

  3. webui layout like desktop rich client

    similarity similarlike desktop js frameworklike extj js frameworklike rich client js frameworkjs lay ...

  4. C++函数指针和指针函数

    本文参考http://www.prglab.com/cms/pages/c-tutorial/advanced-data/pointers.php http://blog.csdn.net/ameyu ...

  5. UIView-图层方法

    // // ViewController.m // UIView-图层概念 // // Created by wangtouwang on 15/5/5. // Copyright (c) 2015年 ...

  6. linux删除、读取文件原理

    linux删除文件原理 LINUX的文件名是存在父目录的block里面,并指向这个文件额inode节点,这个文件的inode节点再标记指向存放这个文件的block的数据块.我们删除一个文件,实际上并不 ...

  7. 【搭建开发环境】在 Windows XP 中参与开源项目,搭建 git 和 cygwin 开发环境

    引言 只有一台 Windows XP 家用机,却想在诸如 Git@OSC 之类的开源社区参与开发,本文提供一个入门级的开发环境搭建指引. 涉及工具:Eclipse,EGit,Cygwin. 欢迎来到 ...

  8. linux强大IDE——Geany配置说明

    今天开始用Ubuntu了(主要是为了防止自己在windows下不自觉的打游戏之类的)   刚开始用的很不习惯  找不到合适的编译器(DEV c++什么时候才能出Linux的啊)  先后下了codeli ...

  9. markdown与textile之间互相转换

    markdown与textile之间互相转换 redmine中默认使用的是textile那么从别的地方复制过来的markdown格式的内容需要进行转换 找到一款工具叫做pandoc http://jo ...

  10. iBatis系列一

    XML iBatis可以使用xml来作为参数输入以及结果返回:这个功能的优势在于某些特定的场景:还有可以通过DOM方式来作为参数传递:但是这个方式应用的比较少,如果服务器是xml服务器可以采用这种方式 ...