题目大意:已知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 背包的更多相关文章

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

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

  2. POJ2184 Cow Exhibition[DP 状态负值]

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12420   Accepted: 4964 D ...

  3. poj2184 Cow Exhibition(p-01背包的灵活运用)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=2184">http://poj.org/problem?id=2184 Descrip ...

  4. poj2184 Cow Exhibition【01背包】+【负数处理】+(求两个变量的和最大)

    题目链接:https://vjudge.net/contest/103424#problem/G 题目大意: 给出N头牛,每头牛都有智力值和幽默感,然后,这个题目最奇葩的地方是,它们居然可以是负数!! ...

  5. POJ-2184 Cow Exhibition---01背包变形(负数偏移)

    题目链接: https://vjudge.net/problem/POJ-2184 题目大意: 给出num(num<=100)头奶牛的S和F值(-1000<=S,F<=1000),要 ...

  6. poj2184 Cow Exhibition

    思路: dp+滚动数组. 类似01背包. 实现: #include <iostream> #include <cstdio> #include <algorithm> ...

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

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

  8. poj 2184 Cow Exhibition(01背包)

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10882   Accepted: 4309 D ...

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

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

随机推荐

  1. Centos7.5 在桌面创建AndroidStudio快捷方式

    Centos7 在桌面创建AndroidStudio快捷方式 前言 最近安装了Centos7,打算将开发平台转移到Linux下,安装好AndroidStudio后,桌面没有快捷方式有些不习惯,随自己创 ...

  2. Visual C++ 6.0的界面介绍

      双击Visual C++ 6.0安装目录下的文件启动Visual C++ 6.0,通过“文件”→“新建”可新建一个Win32 Console Application项目.创建好项目后,显示Visu ...

  3. ANE打包

    哈哈,曾经梦寐以求的ANE终于弄成功了一个.说实话,学java和Android就是为了写ANE!好啦,今天把我体会到的记录一下: 网上其实打包ANE的教程好多,我也找了好多好多.但是好多我自己试了还是 ...

  4. 【sqli-labs】 less36 GET- Bypass MYSQL_real_escape_string (GET型绕过MYSQL_real_escape_string的注入)

    看一下mysql_real_escape_string()函数 \x00 \x1a 注入的关键还是在于闭合引号,同样使用宽字节注入 http://192.168.136.128/sqli-labs-m ...

  5. matlab 读取输入数组

    In an assignment A(I) = B, the number of elements in B and I must be the same MATLAB:index_assign_el ...

  6. 08.Web服务器-2.HTTP协议介绍

    HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写.它的发展是万维网协会(World Wide Web Consortium)和Internet工作小组IETF ...

  7. 20.基于es内部_version进行乐观锁并发控制

  8. python爬虫08 | 你的第二个爬虫,要过年了,爬取豆瓣最受欢迎的250部电影慢慢看

    马上就要过年啦 过年在家干啥咧 准备好被七大姑八大姨轮番「轰炸」了没? 你的内心 os 是这样的 但实际上你是这样的 应付完之后 闲暇时刻不妨看看电影 接下来咱们就来爬取豆瓣上评分最高的 250部电影 ...

  9. Python语言数据结构和语言结构(2)

    目录 1. Python预备基础 2. Python数据类型 3. Python条件语句 4. while循环和for循环 1. Python预备基础 1.1 变量的命名   变量命名规则主要有以下几 ...

  10. 快速上手Linux 玩转典型应用_慕课网笔记

    1.没有exe安装程序 2.区分大小写 3.一切皆文件 4.文件后缀不是那么重要,只是为了好识别 -------------------------------------------------- ...