poj2184 Cow Exhibition(p-01背包的灵活运用)
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
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
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <climits>
#include <ctype.h>
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define PI acos(-1.0)
#define INF 0x3fffffff
int MAX(int a,int b)
{
if( a > b)
return a;
return b;
}
int main()
{
int s[147],f[147],dp[200047];
int N,i,j;
while(~scanf("%d",&N))
{
for(i = 0 ; i <= 200000 ; i++)
{
dp[i] =-INF;
}
for(i = 1 ; i <= N ; i++)
{
scanf("%d%d",&s[i],&f[i]);
}
dp[100000] = 0;//初始化dp[100000]相当于“dp[0]”也就是智力和为零的时候
for(i = 1 ; i <= N ; i++)
{
if(s[i] < 0 && f[i] < 0)
continue;
if(s[i] > 0)//假设s[i]为正数。那么我们就从大的往小的方向进行背包
{
for(j = 200000 ; j >= s[i] ; j--)//100000以上是智力和为正的时候
{
if(dp[j-s[i]] > -INF)
{
dp[j]=MAX(dp[j],dp[j-s[i]]+f[i]);
}
}
}
else//假设s[i]为负数。那么我们就从小的往大的方向进行背包
{
for(j = s[i] ; j <= 200000+s[i] ; j++)//100000一下是当智力和为负的时候
{
if(dp[j-s[i]] > -INF)
{
dp[j]=MAX(dp[j],dp[j-s[i]]+f[i]);
}
}
}
}
int ans=-INF;
for(i = 100000 ; i <= 200000 ; i++)//仅仅在智力和为正的区间查找智力和幽默值的和最大的
{
if(dp[i]>=0)
{
ans=MAX(ans,dp[i]+i-100000);
}
}
printf("%d\n",ans);
}
return 0;
}
poj2184 Cow Exhibition(p-01背包的灵活运用)的更多相关文章
- poj2184 Cow Exhibition【01背包】+【负数处理】+(求两个变量的和最大)
题目链接:https://vjudge.net/contest/103424#problem/G 题目大意: 给出N头牛,每头牛都有智力值和幽默感,然后,这个题目最奇葩的地方是,它们居然可以是负数!! ...
- 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 ...
- USACO 2003 Fall Orange Cow Exhibition /// 负数01背包 oj22829
题目大意: 输入n 接下来n行 每行输入 a b 输出n行中 a+b总和最大的同时满足 所有a总和>=0所有b总和>=0的值 负数的01背包应该反过来 w[i]为正数时 需要从大往小推 即 ...
- POJ 2184:Cow Exhibition(01背包变形)
题意:有n个奶牛,每个奶牛有一个smart值和一个fun值,可能为正也可能为负,要求选出n只奶牛使他们smart值的和s与fun值得和f都非负,且s+f值要求最大. 分析: 一道很好的背包DP题,我们 ...
- Cow Exhibition (01背包)
"Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with G ...
- PKU--2184 Cow Exhibition (01背包)
题目http://poj.org/problem?id=2184 分析:给定N头牛,每头牛都有各自的Si和Fi 从这N头牛选出一定的数目,使得这些牛的 Si和Fi之和TS和TF都有TS>=0 F ...
- POJ-2184 Cow Exhibition(01背包变形)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10949 Accepted: 4344 Descr ...
- POJ2184 Cow Exhibition 背包
题目大意:已知c[i]...c[n]及f[i]...f[n],现要选出一些i,使得当sum{c[i]}和sum{f[i]}均非负时,sum(c[i]+f[i])的最大值. 以sum(c[i])(c[i ...
随机推荐
- 某考试 T1 lcm
把lcm写成 (a+n)*(b+n) / gcd(a+n,b+n). 因为gcd可以辗转相减,所以就成了gcd(abs(a-b),a+n),一个常量一个变量之间的gcd,我们可以直接把abs(a-b) ...
- 【DFS】Codeforces Round #398 (Div. 2) C. Garland
设sum是所有灯泡的亮度之和 有两种情况: 一种是存在结点U和V,U是V的祖先,并且U的子树权值和为sum/3*2,且U不是根,且V的子树权值和为sum/3. 另一种是存在结点U和V,他们之间没有祖先 ...
- 【周期性/容斥+二分】POJ2773-HAPPY 2006
[题目大意] 求与n互质的第k个数. [思路] 先求出小于k且与n互质的数,再利用gcd(bt+a,b)=gcd(a,b)的性质求解,效率低.枚举与n互质的数的效率是O(nlogn),求解第k个数的效 ...
- Problem A: 零起点学算法16——鸡兔同笼
#include<stdio.h> int main() { int n,m,a,b; while(scanf("%d %d",&n,&m)!=EOF) ...
- Android 获取 json
Android 获取 json MainActivity.java package com.example.jsontest; import java.io.IOException; import ...
- cs-Filters
ylbtech-Unitity: cs-Filters HealthcareAuthorizeAttribute.cs HealthcareHandleErrorAttribute.cs Health ...
- 关于Linux开源项目基础组件make编译流程
关于Linux开源项目基础组件make编译流程 非常多Linux开源项目都会用到编译出可运行文件的make.这个是有一套流程的. 首先,GNU构建系统:https://en.wikipedia. ...
- Selenium webdriver Java 开始
最早接触的selenium是 selenium IDE,当时是为了准备论文.为了用IDE还下载了Firefox浏览器.后来接触过两个项目都需要selenium,一个采用selenium webdirv ...
- javascript - 闭包之一些常见的写法
/** * 防御型分号 */ ; (function () { //code.... console.log('一开始,我自己执行了!!!'); })(); /** * 形参 -> 实参 * 定 ...
- 使用Python实现生产者消费者问题
之前用C++写过一篇生产者消费者的实现. 生产者和消费者主要是处理互斥和同步的问题: 队列作为缓冲区,需要互斥操作 队列中没有产品,消费者需要等待,直到生产者放入产品并通知它.队列慢的情况类似. 这里 ...