简单的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. PlayerPrefs存储Vector3等结构数据

     孙广东   2016.3.31 之前有网友询问这个问题, 当时我仅仅是 提供了一个思路, 使用json序列化.反序列化. 以下就写出两种(都是仅仅能变成字符串) 1.json的序列化.反序列化方 ...

  2. vs2013+ffmpeg开发环境搭建【转】

    本文转载自:http://blog.csdn.net/spaceyqy/article/details/43115391 每当看到配环境,我就泪流满面,好吧,闲话不多说,进入正题. 1.去官方下载ff ...

  3. HDU1412 {A} + {B}

    2019-05-17 10:15:01 每个元素之间有一个空格隔开. 每行最后一的元素后面没有空格,区别于HDU人见人爱A - B 注意使用STL的时候要清空 .  a.clear(); #inclu ...

  4. Npgsql使用入门(三)【批量导入数据】

    Program.cs代码: class Program { static void Main(string[] args) { var test = new PgBulkCopyHelper<S ...

  5. vue-router 原理解析

    “更新视图但不重新请求页面”是前端路由原理的核心之一,

  6. Vue-router记录

    一.嵌套路由默认选中第一个子路由 可以给主路由加一个重定向的属性,把路径指向相应的子路由. { path: '/home', name: 'Home', //重定向 redirect: '/home/ ...

  7. 学习js与css 写个2048

    学习阶段,还是写点小东西练练手学的有意思一点,今天用栅格布局做了一个2048,但是移动动画和合并特效没有做,只简单的实现了一下功能. 记录一下学习的过程. 1.入口函数,初始化界面,我这里是直接是一个 ...

  8. 自定义安装MS Office Project2007会出错

    作者:朱金灿 来源:http://blog.csdn.net/clever101 今天使用虚拟光驱文件自定义安装MSOffice Project2007,如下图: 然后总是出现一个错误: 从网上找来一 ...

  9. 用JS解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题

    当用ajax异步时,返回JsonResult格式的时候,发现当字段是dateTime类型时,返回的json格式既然是“/Date(1435542121135)/” 这样子的,当然这不是我们想要的格式. ...

  10. PythonGIS可视化—Matplot basemap工具箱

    原文链接:http://www.douban.com/group/topic/32821988/ 原文链接:http://www.cnblogs.com/vamei/archive/2012/09/1 ...