题目链接:http://poj.org/problem?id=2184

Cow Exhibition
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9479   Accepted: 3653

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

题目大意:
    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背包变形)的更多相关文章

  1. POJ 2184 Cow Exhibition【01背包+负数(经典)】

    POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...

  2. poj 2184 Cow Exhibition(01背包)

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10882   Accepted: 4309 D ...

  3. POJ 2184 Cow Exhibition (01背包变形)(或者搜索)

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10342   Accepted: 4048 D ...

  4. poj 2184 Cow Exhibition(dp之01背包变形)

    Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...

  5. POJ 2184:Cow Exhibition(01背包变形)

    题意:有n个奶牛,每个奶牛有一个smart值和一个fun值,可能为正也可能为负,要求选出n只奶牛使他们smart值的和s与fun值得和f都非负,且s+f值要求最大. 分析: 一道很好的背包DP题,我们 ...

  6. POJ 2184 Cow Exhibition (01背包的变形)

    本文转载,出处:http://www.cnblogs.com/Findxiaoxun/articles/3398075.html 很巧妙的01背包升级.看完题目以后很明显有背包的感觉,然后就往背包上靠 ...

  7. poj 2184 Cow Exhibition(背包变形)

    这道题目和抢银行那个题目有点儿像,同样涉及到包和物品的转换. 我们将奶牛的两种属性中的一种当作价值,另一种当作花费.把总的价值当作包.然后对于每一头奶牛进行一次01背包的筛选操作就行了. 需要特别注意 ...

  8. POJ 2184 Cow Exhibition 奶牛展(01背包,变形)

    题意:有只奶牛要证明奶牛不笨,所以要带一些奶牛伙伴去证明自己.牛有智商和幽默感,两者可为负的(难在这),要求所有牛的智商和之 / 幽默感之和都不为负.求两者之和的最大值. 思路:每只牛可以带或不带上, ...

  9. POJ 2184 Cow Exhibition 01背包

    题意就是给出n对数 每对xi, yi 的值范围是-1000到1000 然后让你从中取若干对 使得sum(x[k]+y[k]) 最大并且非负   且 sum(x[k]) >= 0 sum(y[k] ...

随机推荐

  1. PHP学习笔记12-上传文件

    上传图片文件并在页面上显示出图片 enctype介绍:enctype属性指定将数据发回到服务器时浏览器使用的编码类型. 取值说明: multipart/form-data: 窗体数据被编码为一条消息, ...

  2. 【转】vs2008中leptonica-1.68安装配置

    tesseract ocr挺不好配置的,找到一篇不错的文章,分享如下:http://hi.baidu.com/ever8936/blog/item/6998e1196b1d0161dab4bd8f.h ...

  3. python OptionParser模块

    Python中强大的选项处理模块. #!/usr/bin/python from optparse import OptionParser parser = OptionParser() parser ...

  4. perl5 第二章 简单变量

    第二章 简单变量  by flamephoenix 一.整型 二.浮点数 三.字符串 基本上,简单变量就是一个数据单元,这个单元可以是数字或字符串.一.整型 1.整型   PERL最常用的简单变量,由 ...

  5. kinect for windows - SkeletonBasics-D2D详解之二

    首先看骨骼追踪例子代码的结构:

  6. HDOJ 1427(dfs) 速算24点

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1427 思路分析: 题目要求判断是否存在一种运算组合使得4个数的计算结果为24,因为搜索的层次为3层,不 ...

  7. xen虚拟机操作整理

    1,登陆物理机器 2,查看物理机建立虚拟机的列表 root:~ # xm li Name ID Mem VCPUs State Time(s) Domain-0 0 49450 8 r----- 52 ...

  8. android基础篇------------java基础(12)(多线程操作)

    <一>基本概念理解 1.什么是进程? 进程就是在某种程度上相互隔离,独立运行的程序.一般来说,系统都是支持多进程操作的,这所谓的多进程就是让系统好像同时运行多个程序. 2.什么是线程呢? ...

  9. 简单的前端js+ajax 购物车框架(入门篇)

    其实,一直想把自己写的一些js给总结下,也许是能力有限不能把它完美结合起来.只能自己默默的看着哪些代码,无能为力. 今天在公司实在没有事做,突然就想到写下商城的购物车的前端框架,当然我这里只有购物车的 ...

  10. [Swust OJ 794]--最近对问题(分治)

    题目链接:http://acm.swust.edu.cn/problem/794/ Time limit(ms): 1000 Memory limit(kb): 10000   Description ...