Cow Exhibition (背包中的负数问题)
个人心得:背包,动态规划真的是有点模糊不清,太过于抽象,为什么有些是从后面递推,
有些状态就是从前面往后面,真叫人头大。
这一题因为涉及到负数,所以网上大神们就把开始位置从10000开始,这样子就转变为了由一个正数背包装的最大值构成的背包问题了,
只要对于正数背包中的容积加价值相加就可以了,还是不太了解,有点模糊不清楚的感觉;
这题还要注意当背包容积大于0小于0的时候的递推,大于0是从后面往前面走,小于0反之。
大于0就是纯粹的01背包问题,和模板一样的吧,小于0的话就好像多重背包吧,可以覆盖,所以就从前面开始递推吧,脑阔痛。
fun..."
- Cows with Guns by Dana Lyons
The cows want to prove to the public that they are both smart and fun. In order to do this, Bessie has organized an exhibition that will be put on by the cows. She has given each of the N (1 <= N <= 100) cows a thorough interview and determined two values for each cow: the smartness Si (-1000 <= Si <= 1000) of the cow and the funness Fi (-1000 <= Fi <= 1000) of the cow.
Bessie must choose which cows she wants to bring to her exhibition. She believes that the total smartness TS of the group is the sum of the Si's and, likewise, the total funness TF of the group is the sum of the Fi's. Bessie wants to maximize the sum of TS and TF, but she also wants both of these values to be non-negative (since she must also show that the cows are well-rounded; a negative TS or TF would ruin this). Help Bessie maximize the sum of TS and TF without letting either of these values become negative.
Input
* Lines 2..N+1: Two space-separated integers Si and Fi, respectively the smartness and funness for each cow.
Output
Sample Input
5
-5 7
8 -6
6 -3
2 1
-8 -5
Sample Output
8
Hint
Bessie chooses cows 1, 3, and 4, giving values of TS = -5+6+2 = 3 and TF
= 7-3+1 = 5, so 3+5 = 8. Note that adding cow 2 would improve the value
of TS+TF to 10, but the new value of TF would be negative, so it is not
allowed.
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int money=;
const int inf=;
int cows[],cowa[];
int dp[];
void init(){
for(int i=;i<=;i++)
dp[i]=-inf;
dp[]=;
}
int main(){
int t;
while(cin>>t){
init();
for(int i=;i<=t;i++)
{
cin>>cows[i]>>cowa[i]; }
int maxn=-inf;
for(int i=;i<=t;i++)
{
if(cows[i]<&&cowa[i]<)
continue;
if(cows[i]>){
for(int j=;j>=cows[i];j--)
if(dp[j-cows[i]]>-inf)
dp[j]=max(dp[j],dp[j-cows[i]]+cowa[i]);
}
else
{
for(int j=;j<=+cows[i];j++)
if(dp[j-cows[i]]>-inf)
dp[j]=max(dp[j],dp[j-cows[i]]+cowa[i]);
} }
for(int i=;i<=;i++)
if(dp[i]>=) maxn=max(maxn,dp[i]+i-);
cout<<maxn<<endl;
}
return ;
}
Cow Exhibition (背包中的负数问题)的更多相关文章
- POJ-2184 Cow Exhibition---01背包变形(负数偏移)
题目链接: https://vjudge.net/problem/POJ-2184 题目大意: 给出num(num<=100)头奶牛的S和F值(-1000<=S,F<=1000),要 ...
- 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 ...
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
- poj2184 Cow Exhibition【01背包】+【负数处理】+(求两个变量的和最大)
题目链接:https://vjudge.net/contest/103424#problem/G 题目大意: 给出N头牛,每头牛都有智力值和幽默感,然后,这个题目最奇葩的地方是,它们居然可以是负数!! ...
- poj 2184 Cow Exhibition(dp之01背包变形)
Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...
- POJ-2184 Cow Exhibition(01背包变形)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10949 Accepted: 4344 Descr ...
- POJ 2184 Cow Exhibition (01背包变形)(或者搜索)
Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10342 Accepted: 4048 D ...
- 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 ...
随机推荐
- 交叉编译Mesa,X11lib,Qt opengl
记录Mesa配置文件如下: Mesa版本:Mesa-10.2.3 CC=/usr/local/arm-4.8.1/bin/arm-none-linux-gnueabi-gcc CXX=/usr/loc ...
- gh-ost使用手册
gh-ost实战运用 一.安装步骤 1.环境 go版本:1.10.3 gh-ost版本:1.0.46 2.安装go语言 # 安装go依赖包 yum install bison ed gawk gcc ...
- QGIS 编译
QGIS 编译 在编译的过程中花费了很长时间,特别是编译Debug版本.release版本的编译可以从晚上找到很多的资料,但是Debug的编译相对较少.在Debug编译的过程中,需要单独build工程 ...
- Kubernetes busybox nslookup问题
使用最新版本的busybox会出现nslookup提示无法解析的问题: Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find k ...
- linux 虚拟机在线添加新磁盘
在线添加磁盘,扩展LVM卷案例 一.添加硬盘,在线扫描出来 首先到虚拟机那里添加一块硬盘,注意必须是SCSI类型的硬盘. 扫描硬盘,不用重启操作系统的. echo "- - -" ...
- Elasticsearch6.4.3安装
Linux内存一定要1g以上! 首先要有jdk环境 要求1.8版本以上 elasticsearch是Java写的 将上传的 elasticSearch安装包解压 cd /home/elastics ...
- java的一些基本格式
书写方法的格式: 修饰符 返回值 方法名 方法体 public int/void addNumber(参数) { ...
- MVC 绑定 下拉框数据
HTML: <div class="form-group col-sm-12"> <div class="col-sm-4"> < ...
- bind的原生代码实现
<script> function foo(p1,p2) { this.val = p1 + p2; } var bar = foo.bind(this, "p1"); ...
- 安装Tomcat服务器以及错误汇总(tomcat8.0、jdk8)
Tomcat安装和启动 一.下载Tomcat 你可以直接百度Tomcat官网, 或者,直接在地址栏输入他的官网地址:http://tomcat.apache.org/,然后进入他的主页,在主页左侧可以 ...