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 ...
随机推荐
- indeed 5.13 第二次网测
题目描述,我找不见了,大概写一下想法和代码吧. 1. 没有看 2. 由于数据范围很小,就是简单的枚举,求全排列,然后更新答案. #include<bits/stdc++.h> #defin ...
- asp.net ajax 简单案例
第一步先引用 scriptManager <asp:UpdatePanel ID="UpdatePanelGuanZhu" runat="server"& ...
- Deutsch lernen (12)
1. hinweisen - wies hin - hingewiesen 向...指出,指明 auf etw.(A) hinweisen Ich möchte (Sie) darauf hiweis ...
- Interrupt中断线程
package com.wistron.swpc.ecs.util; public class WrongWayStopThread extends Thread{ public static voi ...
- FormCollection获取请求数据
public ActionResult Add(FormCollection fm) //通过FormCollection 对象获取表单数据 { string message = "&quo ...
- Python—字符串+变量
字符串转义字符格式化内建函数(后延)转义字符用一个特殊的方法表示出一系列不方便写出的内容,比如回车键,换行键,退格键借助反斜杠字符,一旦字符串中出现反斜杠,则反斜杠后面一个火几个字符表示已经不是原来的 ...
- [接口管理平台] eoLinker AMS 专业版 V3.5 :加入数据结构管理、通用函数管理、API 快速测试等近 30 项更新
eoLinker AMS是集API文档管理.API自动化测试.开发协作三位一体的综合API开发管理平台,是中国最大的在线API管理平台.目前eoLinker AMS已经为来自全球的超过两万家企业托管超 ...
- ZooKeeper 运维经验
转自:http://www.juvenxu.com/2015/03/20/experiences-on-zookeeper-ops/ ZooKeeper 运维经验 ZooKeeper 是分布式环境下非 ...
- [luogu4290 HAOI2008]玩具取名(DP)
传送门 Solution 裸区间DP Code #include <map> #include <cmath> #include <cstdio> #include ...
- Atcoder AGC031C Differ By 1 Bit (构造、二进制)
哎呀这个C怎么比B还水....(我现在大概也就会做点这种水题了吧) 题目链接 https://atcoder.jp/contests/agc031/tasks/agc031_c 题目大意 符号约定: ...