题意:

  有一些牛,每头牛有一个Si值,一个Fi值,选出一些牛,使得max( sum(Si+Fi) ) 并且 sum(Si)>=0, sum(Fi)>=0

思路:

  随便选一维做容量(比如Fi),另一维做价值,然后直接做01背包。

  做的时候注意一下方向。

  最后,在合法解里面找一下最优解就好了。

代码:

  

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <cctype>
#include <time.h> using namespace std; typedef pair<int, int> PII; const int INF = 1e6;
const int MAXM = 1e5+;
const int MAXN = ; PII a[MAXN];
int dp[MAXM];
int n; int main() {
#ifdef Phantom01
freopen("PKU2184.txt", "r", stdin);
#endif //Phantom01 while (scanf("%d", &n)!=EOF) {
int sum = ;
for (int i = ; i < n; i++) {
scanf("%d%d", &a[i].first, &a[i].second);
if (a[i].first>) sum += a[i].first;
}
sort(a, a+n);
for (int i = ; i <= sum; i++) dp[i] = -INF;
dp[] = ;
for (int i = n-; i >= ; i--) {
int f = a[i].first, s = a[i].second;
if (f< && s<) continue;
if (f>=) {
for (int j = sum-f; j >= ; j--) {
dp[j+f] = max(dp[j+f], dp[j]+s);
}
} else {
for (int j = -f; j <= sum; j++) {
dp[j+f] = max(dp[j+f], dp[j]+s);
}
}
}
int ans = ;
for (int i = ; i <= sum; i++) if (dp[i]>=) {
ans = max(ans, i+dp[i]);
}
printf("%d\n", ans);
} return ;
}

写的时候又一次为了剪枝,不小心删掉了一些合法解,甚至是最优解,然后wa了两发……真是伤心

PKU 2184 Cow Exhibition 01背包的更多相关文章

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

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

  2. POJ 2184 Cow Exhibition (01背包变形)(或者搜索)

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10342   Accepted: 4048 D ...

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

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

  4. POJ 2184 Cow Exhibition 01背包

    题意就是给出n对数 每对xi, yi 的值范围是-1000到1000 然后让你从中取若干对 使得sum(x[k]+y[k]) 最大并且非负   且 sum(x[k]) >= 0 sum(y[k] ...

  5. POJ-2184 Cow Exhibition(01背包变形)

    Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10949 Accepted: 4344 Descr ...

  6. poj 2184 Cow Exhibition(背包变形)

    这道题目和抢银行那个题目有点儿像,同样涉及到包和物品的转换. 我们将奶牛的两种属性中的一种当作价值,另一种当作花费.把总的价值当作包.然后对于每一头奶牛进行一次01背包的筛选操作就行了. 需要特别注意 ...

  7. POJ 2184 Cow Exhibition(背包)

    希望Total Smart和Totol Funess都尽量大,两者之间的关系是鱼和熊掌.这种矛盾和背包的容量和价值相似. dp[第i只牛][j = 当前TotS] = 最大的TotF. dp[i][j ...

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

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

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

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

随机推荐

  1. WoSign全球可信网站安全认证签章安装指南

    您购买了WoSign SSL证书后,将免费获得一个能直观地显示贵网站的认证信息的可信网站安全认证标识,能大大增强用户的在线信任,促成更多在线交易.所以,建议您在安装成功SSL证书后马上在网站的首页和其 ...

  2. 全球NTP服务器列表

    全球- pool.ntp.org 欧洲- europe.pool.ntp.org 亚洲- asia.pool.ntp.org 大洋洲- oceania.pool.ntp.org 北美- north-a ...

  3. docker mysql pxc集群(percona-xtradb-cluster)

    docker pull percona/percona-xtradb-cluster docker tag percona/percona-xtradb-cluster pxc docker netw ...

  4. BZOJ 3413 匹配 (后缀自动机+线段树合并)

    题目大意: 懒得概括了 神题,搞了2个半晚上,还认为自己的是对的...一直调不过,最后终于在jdr神犇的帮助下过了这道题 线段树合并该是这道题最好理解且最好写的做法了,貌似主席树也行?但线段树合并这个 ...

  5. [读书笔记] R语言实战 (四) 基本数据管理

    1. 创建新的变量 mydata<-data.frame(x1=c(2,2,6,4),x2=c(3,4,2,8)) #方法一 mydata$sumx<-mydata$x1+mydata$x ...

  6. W10如何开启LinuxBash及安装Ubuntu

    W10如何开启LinuxBash的功能 1)开启开发人员模式 2)启动部分windows功能 完成后重启系统 然后在cmd中输入bash按命令操作即可使用bash命令 3)下载安装ubuntu lxr ...

  7. 高级函数-case

    case函数 (适合区间,>,<判断)    case when 判断表达式 then         when 判断表达式 then         .....    end     s ...

  8. [Angular + Unit Testing] Mock HTTP Requests made with Angular’s HttpClient in Unit Tests

    In a proper unit test we want to isolate external dependencies as much as possible to guarantee a re ...

  9. Session小案例-----简单购物车的使用

    Session小案例-----简单购物车的使用 同上篇一样,这里的处理请求和页面显示相同用的都是servlet. 功能实现例如以下: 1,显示站点的全部商品 2.用户点击购买后,可以记住用户选择的商品 ...

  10. hdu 5335 Walk Out 搜索+贪心

    Walk Out Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total S ...