简单的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的更多相关文章

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

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

  2. poj 2184 01背包变形【背包dp】

    POJ 2184 Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14657   Accepte ...

  3. (01背包变形) Cow Exhibition (poj 2184)

    http://poj.org/problem?id=2184   Description "Fat and docile, big and dumb, they look so stupid ...

  4. [POJ 2184]--Cow Exhibition(0-1背包变形)

    题目链接:http://poj.org/problem?id=2184 Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  5. POJ 2184(01背包)(负体积)

    http://poj.org/problem?id=2184 http://blog.csdn.net/liuqiyao_01/article/details/8753686 对于负体积问题,可以先定 ...

  6. DP:Cow Exhibition(POJ 2184)(二维问题转01背包)

        牛的展览会 题目大意:Bessie要选一些牛参加展览,这些牛有两个属性,funness和smartness,现在要你求出怎么选,可以使所有牛的smartness和funness的最大,并且这两 ...

  7. POJ 2184 01背包+负数处理

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10200   Accepted: 3977 D ...

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

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

  9. poj 2184 Cow Exhibition

    // 给定n头牛,每头有属性智商和幽默感,这两个属性值有正有负,现在要从这n头牛中选出若干头使得他们的智商和与幽默感和不为负数,// 并且两者两家和最大,如果无解输出0,n<=100,-1000 ...

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

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

随机推荐

  1. luogu3369 【模板】 普通平衡树 Splay

    题目大意 维护一个数据结构,满足以下操作: 插入x数 删除x数(若有多个相同的数,因只删除一个) 查询x数的排名(排名定义为比当前数小的数的个数+1.若有多个相同的数,因输出最小的排名) 查询排名为x ...

  2. Visio的安装教程

    1.下载地址:https://msdn.itellyou.cn/ 2.输入关键字:Visio,选择想要下载的版本,展开详细信息,并复制 3.(我下载的13) ed2k://|file|cn_visio ...

  3. 分布式系统CAP原则与BASE思想

    一.CAP原则 CAP原则又称CAP定理,指的是在一个分布式系统中, Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),三者 ...

  4. golang iris下面的websocket

    最近要做后台主动推送:(iris框架,封装的有wesocket,刚开始以为直接拿过来用,结果不是现在贴一下代码,写一下遇到的坑) func main() {    app := iris.New()  ...

  5. C#中的流_字节_字符_字符串之间的相互转换

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  6. 如何运用docker配合python开发

    在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...

  7. mac安装python3 pandas tushare

    1,升级pip python3 -m pip install --upgrade pip 2,安装依赖包 pip install --user numpy scipy jupyter pandas s ...

  8. (转)webpack用法

    前言 webpack前端工程中扮演的角色越来越重要,它也是前端工程化很重要的一环.本文将和大家一起按照项目流程学习使用wbepack,妈妈再也不用担心我不会使用webpack,哪里不会看哪里.这是一个 ...

  9. 打开word2010每次都要配置进度的解决办法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 不小心把ms office2010搞坏了,于是重装ms office2010,结果一打开word文档时总是出现下面的对 ...

  10. 11.javaweb国际化标签库

    一.国际化标签库 1,格式化标签库提供的标签 2,标签详解 2.1<fmt:setLocale>标签 下面设置不同的区域,并在设置的区域下显示日期 2.2<fmt:requestEn ...