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 ...
随机推荐
- Spring AOP 学习(一) 代理模式
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程(也叫面向方面),可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...
- C语言:内存字节对齐详解
转:http://blog.csdn.net/arethe/article/details/2548867 一.什么是对齐,以及为什么要对齐: 1. 现代计算机中内存空间都是按照byte划分的,从理论 ...
- Dual Boot WINDOWS 10 and KALI LINUX Easily STEP BY STEP GUIDE截图
mark. kali安装:https://www.youtube.com/watch?v=KLj2yQPWZDk 删除无用分区:http://www.xitongcheng.com/jiaocheng ...
- linux与windows 通过SecureCRT进行文件传输方式
linux与windows 通过SecureCRT进行文件传输方式 方式一:lrzsz是一款在Linux里可代替ftp上传和下载的程序.(小文件推荐,以4G为界限) # rz -bash: rz: c ...
- BufferingForwardingAppender in log4net
https://blog.csdn.net/szx1999/article/details/50073857 7. 写日志会影响系统性能吗? 写日志必然是会消耗一定资源的,而RollingFileAp ...
- Android fill_parent和wrap_content分析
fill_parent设置一个顶部布局或控件强制性让它布满整个屏幕.(这是不管内容大小,直接适应整个屏幕的大小,例长度设置了这,就只有长度适应屏幕的长度) wrap_content布局指根据视图内部内 ...
- QT 实现按住鼠标左键点击对话框空白处,拖动对话框
定义头文件 QPoint move_point; //移动的距离 bool mouse_press; //按下鼠标左键 protected: void mousePressEvent( ...
- Python中MySQL插入数据
sql = 'INSERT INTO course(class_name, credit, properties, teacher_name, college_given, classroom) ' ...
- sql 触发器回顾
一: 触发器是一种特殊的存储过程﹐它不能被显式地调用﹐而是在往表中插入记录﹑更新记录或者删除记录时被自动地激活.所以触发器可以用来实现对表实施复杂的完整性约束. 二: SQL Server为每个触发器 ...
- Windods7+Anaconda+Tensorflow安装步骤
1.下载及安装Anaconda Anaconda是python科学计算的集成.下载Anaconda,下载地址:http://continuum.io/downloads. 由于tensorflow目前 ...