题目大意:已知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. [Luogu 2216] [HAOI2007]理想的正方形

    [Luogu 2216] [HAOI2007]理想的正方形 题目描述 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 输入输出格式 输 ...

  2. wap 5.23 网测几道题目

    1. n个犯人,m个省份, 如果相邻的2个犯人来自同一省份,则是不安全的,求不安全的个数. 正难则反,用全部的个数减去非法的个数,就是最后的答案. m^n - m * (m - 1) ^ (n - 1 ...

  3. window.dialogArguments

    弹出子窗口window.showModalDialog( url, window ); 然后在弹出的子窗口中: window.dialogArguments 即为父窗口window对象的引用.想搞什么 ...

  4. angular js 公告墙

    <!DOCTYPE html>   <html lang="en">   <head>   <meta charset="UTF ...

  5. 【SQL】含有NULL值的排序

    查询结果中有NULL值,当进行升序排序时,NULL值默认为“最大值”,排在最后面.要想改变NULL值的显示顺序,只需要在SQL语句后面加上NULLS FIRST(排在前面),NULLS LAST(排在 ...

  6. MVC5+EasyUI+EF6+Linq通用权限系统出炉--登录(2)

    1.输入验证码后 自动识别验证码并登录.

  7. Swift Method Dispatching — a summary of my talk at Swift Warsaw

    Swift Method Dispatching When announcing Swift, Apple described it as being much faster than Objecti ...

  8. 进行https通信时服务器端下发的是一个证书链

    进行https通信时服务器端下发的是一个证书链,否则无法验证证书的有效性.

  9. C# 判断字符串是否左包含

    //测试字符串 左包含 //string str = "AAABBBCCC"; //char[] ss = str.ToArray(); //0-8 字符数组 //char[] s ...

  10. JavaScript for循环元素取下标问题

    <ul> <li>fg</li> <li>gd</li> <li>gds</li> <li>ghe< ...