POJ2184 Cow Exhibition 背包
题目大意:已知c[i]...c[n]及f[i]...f[n],现要选出一些i,使得当sum{c[i]}和sum{f[i]}均非负时,sum(c[i]+f[i])的最大值。
以sum(c[i])(c[i]>=0)作为背包容量totV,i当作物体,c[i]当作物体体积,f[i]当作物体重量,01背包后,求max{j+DP[j]} (非负)即可。
注意:
- 这里物体体积存在负数,所以循环j时起始条件不能为j=0!而应当在for下面加if,判断子问题在minJ~maxJ范围之内。
- 初值不是DP[最左端点]=0,而是DP[0点所在位置]=0。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int MAX_V = 200010, MAX_OBJ = 110;
#define M(x) x+offset//move int DP(int minJ, int maxJ, int totObj, int *_v, int *_w)
{
static int DP[MAX_V];
int offset = max(-minJ, 0);
memset(DP, 0xcf, sizeof(DP));
DP[M(0)] = 0;
for (int i = 1; i <= totObj; i++)
{
if (_v[i] <= 0)
{
for (int j = minJ; j <= maxJ; j++)
if (j >= minJ + _v[i] && j <= maxJ + _v[i])
DP[M(j)] = max(DP[M(j)], DP[M(j - _v[i])] + _w[i]);
}
else
{
for (int j = maxJ; j >= minJ; j--)
if (j >= minJ + _v[i] && j <= maxJ + _v[i])
DP[M(j)] = max(DP[M(j)], DP[M(j - _v[i])] + _w[i]);
}
}
int ans = 0;
for (int j = 0; j <= maxJ; j++)
if(DP[M(j)] >=0)
ans = max(ans, j+DP[M(j)]);
return ans;
} int main()
{
#ifdef _DEBUG
freopen("c:\\noi\\source\\input.txt", "r", stdin);
#endif
static int _s[MAX_OBJ], _f[MAX_OBJ];
int n, minS = 0, maxS = 0, cnt = 0, s, f, tempAns = 0;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &s, &f);
if (s >= 0 && f >= 0)
{
tempAns += s + f;
continue;
}
else if (s <= 0 && f <= 0)
continue;
else
{
cnt++;
_s[cnt] = s;
_f[cnt] = f;
_s[cnt] > 0 ? maxS += _s[cnt] : minS += _s[cnt]; }
}
printf("%d\n", tempAns + DP(minS, maxS, cnt, _s, _f));
return 0;
}
POJ2184 Cow Exhibition 背包的更多相关文章
- POJ-2184 Cow Exhibition(01背包变形)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10949 Accepted: 4344 Descr ...
- POJ2184 Cow Exhibition[DP 状态负值]
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12420 Accepted: 4964 D ...
- poj2184 Cow Exhibition(p-01背包的灵活运用)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=2184">http://poj.org/problem?id=2184 Descrip ...
- poj2184 Cow Exhibition【01背包】+【负数处理】+(求两个变量的和最大)
题目链接:https://vjudge.net/contest/103424#problem/G 题目大意: 给出N头牛,每头牛都有智力值和幽默感,然后,这个题目最奇葩的地方是,它们居然可以是负数!! ...
- POJ-2184 Cow Exhibition---01背包变形(负数偏移)
题目链接: https://vjudge.net/problem/POJ-2184 题目大意: 给出num(num<=100)头奶牛的S和F值(-1000<=S,F<=1000),要 ...
- poj2184 Cow Exhibition
思路: dp+滚动数组. 类似01背包. 实现: #include <iostream> #include <cstdio> #include <algorithm> ...
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
- poj 2184 Cow Exhibition(01背包)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10882 Accepted: 4309 D ...
- [POJ 2184]--Cow Exhibition(0-1背包变形)
题目链接:http://poj.org/problem?id=2184 Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total S ...
随机推荐
- struts2标签(五)
标签体系结构 jsp出现目的是为了取代servlet,结果逻辑代码,数据库代码都放到了jsp页面中. 为了解决jsp中代码过多的问题,struts2标签分为普通标签和UI标签. 使用struts2标签 ...
- 关于Membership和身份认证的记录
在今天写好的code中测试环节,当我用webconfig中的测试数据库就是ok的,但是更替为正式的就不行了: 报错的类是MemberShip,那就关系到身份认证的环节了 找了几个链接,记录下 1.身份 ...
- MxNet : use the MxNet windows versioin
The MxNet needs the following thirdparties: 1. lapack complie lapack-3.6.1: download the lapack-3.6 ...
- 【sqli-labs】 less38 GET -Stacked Query Injection -String based (GET型堆叠查询字符型注入)
这个直接用union select就可以 http://192.168.136.128/sqli-labs-master/Less-38/?id=0' union select 1,2,3%23 看一 ...
- python核心编程中的对象值比较VS对象身份比较(转载)
转载地址: https://blog.csdn.net/Mluka/article/details/51076786 在python核心编程第四章中,P69在优化下面这段代码时提出了:对象值比较VS对 ...
- windows控制台(console)乱码
在cmd中输入 CHCP 65001,实操有效.记录一下 永久设置,代码如下: Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Cons ...
- Django 模型层(标签、过滤器、模板的继承与导入)
过滤器/自定义过滤器 模板语法中的过滤器类似于python中的内置方法,在我们把数据从后端通过rander传入到前端html文件中之后,在前端我们可以通过模板语法,对传入的数据再进行以通骚操作. 首先 ...
- vue 导航菜单默认子路由
export default new Router({ routes: [ { path: '/', name: 'index', component: index, children: [ { pa ...
- eas更改用户组织范围和业务组织范围
表: T_PM_OrgRangeIncludeSubOrg 10 20 30 分别代表 业务组织 行政组织 以及管辖组织.查行政组织,
- MAC 快捷键&使用技巧等
查看端口占用:命令 lsof -i tcp:port (port替换成端口号,比如6379)可以查看该端口被什么程序占用,并显示PID,方便KILL