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 ...
随机推荐
- Python框架之Tornado(二)预备知识epoll最好的讲解
问:epoll 或者 kqueue 的原理是什么?为什么 epoll 和 kqueue 可以用基于事件的方式,单线程的实现并发?我没看过 linux 内核,对这方面一直有疑问…… 必须从很多基础的概念 ...
- sublime text - vintage
使用sublime text的vim模式的同学注意了: { "color_scheme": "Packages/Color Scheme - Default/Mac Cl ...
- iOS获取某个日期后n个月的日期
一.给一个时间,给一个数,正数是以后n个月,负数是前n个月: -(NSDate *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(NSI ...
- debug(实验)
一.用到的简单的DOS命令: cd\ ——首先要用cd\ 退回到根目录C>下 dir ——显示文件列表 md hb ——建立hb子目录 cd hb ——进入hb子目录 copy d:\dos\m ...
- Kubernetes Ingress
Kubernetes关于服务的暴露主要是通过NodePort方式,通过绑定node主机的某个端口,然后进行pod的请求转发和负载均衡,但这种方式下缺陷是 Service可能有很多个,如果每个都绑定一个 ...
- STAR manual
来源:STARmanual.pdf 来源:Calling variants in RNAseq PART0 准备工作 #STAR 安装前的依赖的工具 #Red Hat, CentOS, Fedora. ...
- java深入探究12-框架之Hibernate
1.引入SSH框架 Struts框架,基于MVC 模式的应用层框架技术 Hibernate,基于持久层框架(数据访问层使用) Dao代码编写的几种方式: 1.原始jdbc操作,Connection/S ...
- MapReduce-从HBase读取数据处理后再写入HBase
MapReduce-从HBase读取处理后再写入HBase 代码如下 package com.hbase.mapreduce; import java.io.IOException; import o ...
- 如何破解mssql for linux 3.5G内存的限制
在上有篇博客中主要介绍了如何在CentOS 中安装和配置mssql ,在安装过程中遇到3.5G内存的限制,下面介绍如何去破解, 微软发布了SQLServer for Linux,但是安装竟然需要3.5 ...
- C++中++i与i++
#include "stdafx.h" #include "string" #include "iostream" #include &qu ...