个人心得:背包,动态规划真的是有点模糊不清,太过于抽象,为什么有些是从后面递推,

有些状态就是从前面往后面,真叫人头大。

这一题因为涉及到负数,所以网上大神们就把开始位置从10000开始,这样子就转变为了由一个正数背包装的最大值构成的背包问题了,

只要对于正数背包中的容积加价值相加就可以了,还是不太了解,有点模糊不清楚的感觉;

这题还要注意当背包容积大于0小于0的时候的递推,大于0是从后面往前面走,小于0反之。

大于0就是纯粹的01背包问题,和模板一样的吧,小于0的话就好像多重背包吧,可以覆盖,所以就从前面开始递推吧,脑阔痛。

"Fat and docile, big and dumb, they look so stupid, they aren't much 
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

* Line 1: A single integer N, the number of cows

* Lines 2..N+1: Two space-separated integers Si and Fi, respectively the smartness and funness for each cow.

Output

* Line 1: One integer: the optimal sum of TS and TF such that both TS and TF are non-negative. If no subset of the cows has non-negative TS and non- negative TF, print 0.

Sample Input

5
-5 7
8 -6
6 -3
2 1
-8 -5

Sample Output

8

Hint

OUTPUT DETAILS:

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 (背包中的负数问题)的更多相关文章

  1. POJ-2184 Cow Exhibition---01背包变形(负数偏移)

    题目链接: https://vjudge.net/problem/POJ-2184 题目大意: 给出num(num<=100)头奶牛的S和F值(-1000<=S,F<=1000),要 ...

  2. 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 ...

  3. POJ 2184 Cow Exhibition【01背包+负数(经典)】

    POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...

  4. poj2184 Cow Exhibition【01背包】+【负数处理】+(求两个变量的和最大)

    题目链接:https://vjudge.net/contest/103424#problem/G 题目大意: 给出N头牛,每头牛都有智力值和幽默感,然后,这个题目最奇葩的地方是,它们居然可以是负数!! ...

  5. poj 2184 Cow Exhibition(dp之01背包变形)

    Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...

  6. POJ-2184 Cow Exhibition(01背包变形)

    Cow Exhibition Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10949 Accepted: 4344 Descr ...

  7. POJ 2184 Cow Exhibition (01背包变形)(或者搜索)

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10342   Accepted: 4048 D ...

  8. poj 2184 Cow Exhibition(01背包)

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10882   Accepted: 4309 D ...

  9. [POJ 2184]--Cow Exhibition(0-1背包变形)

    题目链接:http://poj.org/problem?id=2184 Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total S ...

随机推荐

  1. qt的登录设置(转)

    1.下面添加代码来实现使用用户名和密码登录,这里只是简单将用户名和密码设置为了固定的字符串,如果以后学习了数据库,还可以通过读取数据库来获取用户名和密码.到logindialog.cpp文件中将登录按 ...

  2. Android下Opengl ES实现单屏幕双眼显示

    http://blog.csdn.net/u011371324/article/details/68946779 默认情况下,Opengl ES使用系统提供的帧缓冲区作为绘图表面,一般情况下,如果只在 ...

  3. Qt5.3.0的安装与测试

    Qt5.3.0的安装与测试(交叉编译,用于arm,支持tslib触摸屏) 本次移植可以使用触摸屏. 首先下载源码包: http://download.qt.io/official_releases/q ...

  4. 【转载】OpenWrt sysupgrade 命令行更新固件到最新版

    OpenWrt sysupgrade 命令行更新固件到最新版 下面我们要使用 sysupgrade 更新固件到trunk最新版. 要注意的是,trunk包含试验的功能,可能不稳定,刷机风险自己承担. ...

  5. python中完善decorator

    @decorator可以动态实现函数功能的增加,但是,经过@decorator“改造”后的函数,和原函数相比,除了功能多一点外,有没有其它不同的地方? 在没有decorator的情况下,打印函数名: ...

  6. python日志操作logging

    步骤: 1.定义一个日志收集器 my_logger = logging.getLogger("kitty") 2.设定级别.默认为warning:debug,,info,error ...

  7. maven项目在打war包时出现非法字符: '\ufeff' 解决方案

    问题描述: 开发工具MyEclipse 的总体开发环境,编码格式总体设置为UTF-8,在将web项目打包的时候出现:非法字符:'\ufeff" 错误. 解决方案: 利用notePad++打开 ...

  8. 常用java开发工具快捷键

    在这里列举一些开发中常用的快捷键 常用的idea的快捷键: 1.删除当前行:Ctrl+X 2.格式化代码:Ctrl+Alt+L 3.查看本页里面的内容:Ctrl+F 4.查看类的继承方式:Ctrl+H ...

  9. 多校hdu5754(博弈)

    ©此题中在N×M的棋盘中从(1,1)走到(N,M)B先走G后走,谁先到(N,M)谁赢,走法分为4中分别是国际象棋中的国王,车,马,王后的发,在四种走法下谁能赢: 我们依次分析每一种棋子. ①王. 首先 ...

  10. 泛型学习第三天——C#读取数据库返回泛型集合 把DataSet类型转换为List<T>泛型集合

    定义一个类: public class UserInfo    {        public System.Guid ID { get; set; } public string LoginName ...