POJ 2184
简单的01背包,把S看体积,把F看价值,把它们变正数处理就可以了。在处理负数时,因为减一个负数相当于加一个,所以要从前往后。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string.h>
using namespace std;
int dp[200010];
struct Cow{
int s,f;
}cow[105]; int main(){
int n,positive,negative,sum;
while(scanf("%d",&n)!=EOF){
positive=negative=0;
for(int i=1;i<=n;i++){
scanf("%d%d",&cow[i].s,&cow[i].f);
if(cow[i].s<0)
negative+=(-cow[i].s);
else{
positive+=(cow[i].s);
}
}
for(int i=0;i<200010;i++)
dp[i]=INT_MIN;
int sum=negative+positive;
dp[negative]=0;
for(int i=1;i<=n;i++){
if(cow[i].s<0){
for(int p=-negative;p<=positive;p++){
if(dp[p-cow[i].s+negative]!=INT_MIN&&p-cow[i].s+negative>=0){
dp[p+negative]=max(dp[p+negative],dp[p-cow[i].s+negative]+cow[i].f);
}
}
}
else{
for(int p=positive ;p>=-negative ;p--){
if(dp[p-cow[i].s+negative]!=INT_MIN&&p-cow[i].s+negative>=0){
dp[p+negative]=max(dp[p+negative],dp[p-cow[i].s+negative]+cow[i].f);
}
}
}
}
int ans=INT_MIN;
for(int i=0 ;i<=positive ;i++){
if(dp[i+negative]>=0){
ans=max(ans,i+dp[i+negative]);
}
}
printf("%d\n",ans);
}
return 0;
}
POJ 2184的更多相关文章
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
- poj 2184 01背包变形【背包dp】
POJ 2184 Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14657 Accepte ...
- (01背包变形) Cow Exhibition (poj 2184)
http://poj.org/problem?id=2184 Description "Fat and docile, big and dumb, they look so stupid ...
- [POJ 2184]--Cow Exhibition(0-1背包变形)
题目链接:http://poj.org/problem?id=2184 Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ 2184(01背包)(负体积)
http://poj.org/problem?id=2184 http://blog.csdn.net/liuqiyao_01/article/details/8753686 对于负体积问题,可以先定 ...
- DP:Cow Exhibition(POJ 2184)(二维问题转01背包)
牛的展览会 题目大意:Bessie要选一些牛参加展览,这些牛有两个属性,funness和smartness,现在要你求出怎么选,可以使所有牛的smartness和funness的最大,并且这两 ...
- POJ 2184 01背包+负数处理
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10200 Accepted: 3977 D ...
- POJ 2184 Cow Exhibition (01背包的变形)
本文转载,出处:http://www.cnblogs.com/Findxiaoxun/articles/3398075.html 很巧妙的01背包升级.看完题目以后很明显有背包的感觉,然后就往背包上靠 ...
- poj 2184 Cow Exhibition
// 给定n头牛,每头有属性智商和幽默感,这两个属性值有正有负,现在要从这n头牛中选出若干头使得他们的智商和与幽默感和不为负数,// 并且两者两家和最大,如果无解输出0,n<=100,-1000 ...
- poj 2184 Cow Exhibition(dp之01背包变形)
Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...
随机推荐
- 2016.04.22,英语,《Vocabulary Builder》Unit 17
anim, comes from the Latin anima, meaning 'breath' or 'soul'. animism: ['ænɪmɪzəm] n. 泛灵论,精神存在论,神创宇宙 ...
- 高可用技术工具包 High Availability Toolkit
HighAvailabilityToolkit High Availability Toolkit includes several solutions by which achieving arch ...
- php建立简单的用户留言系统
php建立简单的用户留言系统 样例 addMsg.php--添加留言页面 doAction.php--响应添加留言页面 . viewMsg.php--显示留言页面 目录结构 addMsg.php--添 ...
- 利用Powershell和ceye.io实现Windows账户密码回传
利用Powershell和ceye.io实现Windows账户密码回传 转自:http://www.freebuf.com/articles/system/129068.html 最近在研究Power ...
- 基于scrapy-redis组件的分布式爬虫
scrapy-redis组件安装 分布式实现流程 scrapy-redis组件安装 - 下载scrapy-redis组件:pip install scrapy-redis - 更改redis配置文件: ...
- POJ 2337 欧拉回路
题意: 如果给出的单词能够首尾相接,请按字典序输出单词,中间要加'.' 否则输出三个"*". 思路: 欧拉回路 记得按字典序排序哦~ 加边的时候要倒着加.(邻接表遍历的时候是反着的 ...
- NOIP2013 D1T3 货车运输
[NOIP2013T3]货车运输 背景 noip2013day1 描述 A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重 量限制,简称限重.现在有 q 辆货 ...
- 浅析CLR的异常处理模型
文章目录: 异常概述 CLR中的异常处理机制 CLR中异常的核心类System.Exception类 异常处理的设计规范和最佳实践 异常处理的性能问题 其他拓展 1.异常概述 异常我们通常指的是行动成 ...
- 第4章 部署模式 Three-Tiered Distribution(三级分布)
影响因素 Tiered Distribution 中讨论的影响因素也适用于此模式.有关这些通用影响因素的讨论,请参阅"Tiered Distribution".下列影响因素仅适用于 ...
- wpf ComboBox 获取选中项的文本内容
一:根据数据源类型获取选中项 类: public class Region { public int REGION_ID { get; set; } public string REGION_CODE ...