[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] ...
随机推荐
- BZOJ 1218: [HNOI2003]激光炸弹( 前缀和 + 枚举 )
虽然source写着dp , 而且很明显dp可以搞...但是数据不大 , 前缀和 + 枚举也水的过去..... -------------------------------------------- ...
- C中的正则函数sscanf
头文件 #include 定义函数 int sscanf (const char *str,const char * format,........); 函数说明 sscanf()会将参数str的字符 ...
- 帝国cms语句调用
帝国cms系统,灵动标签,有着非常强大的数据调用功能.这里为广大菜鸟站长普及一下. 我们来看这段代码. [e:loop={,,}] <li>·<a target="_bla ...
- Protel 99SE PCB 打印技巧
1. 打开 Protel99SE PCB 设计文档.从菜单File 下单击Print/Preview 打印预览菜单.出现PCB 打印预览介面. 2.从File 下单击 Setup Printer 设置 ...
- android搭建环境错误 daemon not running. starting it now on port 5037 ADB server didn't ACK
android搭建环境错误 daemon not running. starting it now on port 5037 ADB server didn't ACK ADB server didn ...
- HDU 3350 #define is unsafe
题目大意:给定一个只含有MAX和+操作的式子,求加法运行了多少次,其中MAX使用宏定义. 题解:注意一个规律,对于MAX(A,B)其中A中加a次,B中加b次若A>B,则加a*2+b次,否则a+b ...
- LeetCode——Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- BZOJ2440(全然平方数)二分+莫比乌斯容斥
题意:全然平方数是指含有平方数因子的数.求第ki个非全然平方数. 解法:比較明显的二分,getsum(int middle)求1-middle有多少个非全然平方数,然后二分.求1-middle的非全然 ...
- ASP.net体系
- 阿里云部署 Flask + WSGI + Nginx 详解
抵不住朋友的诱惑,今天终于入手了一台阿里云服务器,是Ubuntu 1.4 32位版本,最初考虑是用来尝尝鲜只是买了个最低配的,价格算起来与在国外买个空间的价格相当吧(可能一年才贵100多),但用起来感 ...