POJ 2184 01背包+负数处理
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10200 | Accepted: 3977 |
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 <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 200005
#define sh 100000
#define inf 999999999 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int dp[N]; struct node{
int v, w;
}a[]; int n; main()
{
int i, j, k;
while(scanf("%d",&n)==){
for(i=;i<n;i++) scanf("%d %d",&a[i].v,&a[i].w);
for(i=;i<N;i++) dp[i]=-inf;
dp[sh]=;
for(i=;i<n;i++){
if(a[i].v>){
for(k=N-;k>=a[i].v;k--) dp[k]=max(dp[k-a[i].v]+a[i].w,dp[k]);
}
else{
for(k=;k<N+a[i].v;k++) dp[k]=max(dp[k-a[i].v]+a[i].w,dp[k]);
}
}
int ans=;
for(i=sh;i<N;i++){
if(dp[i]>) {
ans=max(ans,i-sh+dp[i]);
} }
printf("%d\n",ans);
}
}
POJ 2184 01背包+负数处理的更多相关文章
- poj 2184 01背包变形【背包dp】
POJ 2184 Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14657 Accepte ...
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
- poj 1837 01背包
Balance Time Limit: 1000 MS Memory Limit: 30000 KB 64-bit integer IO format: %I64d , %I64u Java clas ...
- hdu 2184 01背包变形
转自:http://blog.csdn.net/liuqiyao_01/article/details/8753686 题意:这是又是一道01背包的变体,题目要求选出一些牛,使smartness和fu ...
- POJ 3628 01背包 OR 状压
思路: 1.01背包 先找到所有奶牛身高和与B的差. 然后做一次01背包即可 01背包的容积和价格就是奶牛们身高. 最后差值一减输出结果就大功告成啦! 2. 搜索 这思路很明了吧... 搜索的确可以过 ...
- Proud Merchants(POJ 3466 01背包+排序)
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- POJ 3624 01背包
初学DP,用贪心的思想想解题,可是想了一个多小时还是想不出. //在max中的两个参数f[k], 和f[k-weight[i]]+value[i]都是表示在背包容量为k时的最大价值 //f[k]是这个 ...
- POJ之01背包系列
poj3624 Charm Bracelet 模板题 没有要求填满,所以初始化为0就行 #include<cstdio> #include<iostream> using na ...
- (01背包变形) Cow Exhibition (poj 2184)
http://poj.org/problem?id=2184 Description "Fat and docile, big and dumb, they look so stupid ...
随机推荐
- Git 遇到了 early EOF index-pack failed 问题
Git 遇到了 early EOF index-pack failed 问题 今天想 clone 一下 boost 在 github 的 repo,结果在 clone 的过程中遇到了下面的错误.我原本 ...
- 指定YUM安装包的体系结构或版本
在单一体系结构下同时安装32位和64位包echo ‘multilib_policy=all’ >> /etc/yum.conf 指定体系结构 查看当前系统体系结构[root@oracle ...
- SQL优化大全
1. 优化SQL步骤 1. 通过 show status和应用特点了解各种 SQL的执行频率 通过 SHOW STATUS 可以提供服务器状态信息,也可以使用 mysqladmin extende d ...
- centos 更新python
1.CentOS安装Python的依赖包 yum groupinstall "Development tools"yum install zlib-devel bzip2-deve ...
- DIV 垂直 垂直水平 居中
DIV 垂直 居中 让div居中对齐 使用margin-left:auto;margin-right:auto; 可以让你的div居中对齐. .style{margin-left:auto;marg ...
- SSH 无密码远程执行脚本
ssh无密码登录及远程执行脚本要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以CentOS7为例. 测试环境:机器A(10.0.224.80):机器B(192.16 ...
- Java 菜鸟学习之 script脚本语句
脚本语句 一般分为 顺序语句 分支语句 循环语句 顺序语句 就是从上到下依次读取 分支语句 是if语句 if(判断条件) { 满足条件执行的语句 } else { 不满足条件执行的语句 ...
- php常用函数汇总
php常用函数汇总 字符串截取: 1.substr('要截取的字符串','从第几个字符开始','到第几个字符结束'); * 截取英文或者数字 ...
- Animation小问题整理
1.在动画播放中改变层级内容的名字,不会造成动画内容映射的改变. 2.Animator动画剪辑层级没问题,但是不播放 检查是否Mask损坏,FBX文件的Animations选项下面有个Mask.Uni ...
- webapi 解决ajax跨域请求问题
webapi在配置文件中加入这几句就可以解决ajax(同源策略是JavaScript里面的限制,其他的编程语言,比如在C#,Java或者iOS等其他语言中是可以调用外部的WebService,也就是 ...