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 ...
随机推荐
- iOS 关于远程推送(push) 的几个问题
1 push 基本流程原理 (1)启动 app (2)注册远程通知 (3)苹果服务器回调一个deviceToken "didRegisterForRemoteNoti ...
- 【HackerRank】QuickSort(稳定快排,空间复杂度O(n))
QuickSort In the previous challenge, you wrote a partition method to split an array into 2 sub-array ...
- 八、linux优化一
1.关闭selinux sed –I ‘s#SELINUX=enforcing#SELINUX=disabled#g’ /etc/selinux/config grep SELINUX=disable ...
- 20145240《Java程序设计》第七周学习总结
20145240<Java程序设计>第七周学习总结 教材学习内容总结 12.1认识Lambda语法 12.1.1Lambda语法概览 在java中引入了Lambda的同时,与现有API维持 ...
- 吴恩达深度学习笔记(十一)—— dropout正则化
主要内容: 一.dropout正则化的思想 二.dropout算法流程 三.dropout的优缺点 一.dropout正则化的思想 在神经网络中,dropout是一种“玄学”的正则化方法,以减少过拟合 ...
- SecureCRT按退格键出现^H问题
1. 选择选项>>会话选项>>终端>>映射键
- Caused by: org.apache.ibatis.binding.BindingException: Parameter 'parameter' not found.解决
Caused by: org.apache.ibatis.binding.BindingException: Parameter 'company' not found. Available para ...
- Spring初学之Spel初配
Spel又时候可以方便我们为bean的属性赋值,如下配置文件就是常用的一些使用: <?xml version="1.0" encoding="UTF-8" ...
- Tensorflow中使用CNN实现Mnist手写体识别
本文参考Yann LeCun的LeNet5经典架构,稍加ps得到下面适用于本手写识别的cnn结构,构造一个两层卷积神经网络,神经网络的结构如下图所示: 输入-卷积-pooling-卷积-pooling ...
- 智课雅思词汇---十九、前缀se是什么意思
智课雅思词汇---十九.前缀se是什么意思 一.总结 一句话总结:前缀:se- 表示“分开, 离开, 区别开” 前缀:se- [词根含义]:分离 [同源单词]:secede, secession, s ...