简单的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. jcaptcha进阶

    1.改动CaptchaServiceSingleton类.使用带參构造方法来创建DefaultManageableImageCaptchaService对象. watermark/2/text/aHR ...

  2. zjnu 1181 石子合并(区间DP)

    Description 在操场上沿一直线排列着 n堆石子. 现要将石子有次序地合并成一堆.规定每次仅仅能选相邻的两堆石子合并成新的一堆, 并将新的一堆石子数记为该次合并的得分.同意在第一次合并前对调一 ...

  3. Android5.1 在init.rc 中添加自己的服务【转】

    本文转载自:http://blog.csdn.net/VOlsenBerg/article/details/71085610 我有一个需求就是在Android系统开机的时候把一个配置文件放到Andro ...

  4. CNN中的局部连接(Sparse Connectivity)和权值共享

    局部连接与权值共享 下图是一个很经典的图示,左边是全连接,右边是局部连接. 对于一个1000 × 1000的输入图像而言,如果下一个隐藏层的神经元数目为10^6个,采用全连接则有1000 × 1000 ...

  5. JS垃圾回收——和其他语言一样,JavaScript 的 GC 策略也无法避免一个问题:GC 时,停止响应其他操作,这是为了安全考虑

    JavaScript 内存管理 & 垃圾回收机制 标记清除 js 中最常用的垃圾回收方式就是标记清除.当变量进入环境时,例如,在函数中声明一个变量,就将这个而变量标记为“进入环境”.从逻辑上讲 ...

  6. 开源DDos 机器学习思路求解的一些源码——TODO 待分析

    一些源码:https://github.com/elbaulp/MafDet System that aims to detect and mitigate DDoS attacks using Ma ...

  7. Spring中JdbcTemplate中使用RowMapper

    转自:https://blog.csdn.net/u012661010/article/details/70049633 1 sping中的RowMapper可以将数据中的每一行数据封装成用户定义的类 ...

  8. php处理传值为空

    if(isset($_GET['id'])){}else{}isset($_GET['id'])==null

  9. 给<hr/>添加样式

    点线式 破折线式 直线式 双线式 脊线式 槽线式 内嵌效果的 突起效果的 border-top:10px 设置水平线的大小 <hr style=" border-top:5px dot ...

  10. 杭电1019 Least Common Multiple【求最小公倍数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1019 解题思路:lcm(a,b)=a*b/gcd(a,b) 反思:最开始提交的时候WA,以为是溢出了, ...