Description

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

【题意】给出n头牛,分别给出它们的si,fi;取出几头使得si和fi的和最大,并且si之和与fi之和不能为负数;

【思路】背包题,就是存在负数的情况,可以把负数存入下标,数组开大点。用dp[i]存放每个s[i]能得到的最大的f,根据dp的有无,选出最大的dp

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
;
<<;
int dp[N];
],f[];
void init()
{
    ;i<=;i++)
        dp[i]=-inf;
    dp[]=;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        init();
        ;i<=n;i++)
        {
            scanf("%d%d",&s[i],&f[i]);
        }
        ;i<=n;i++)
        {
            &&f[i]<) continue;//两个都为负,没有必要进行下去
            )//si为正,进行从大到小的背包
            {
                ;j>=s[i];j--)
                {
                    if(dp[j-s[i]]>-inf)
                        dp[j]=max(dp[j],dp[j-s[i]]+f[i]);
                }
            }
            else//为负数则从小到大背包
            {
                +s[i];j++)
{ if(dp[j-s[i]]>-inf) dp[j]=max(dp[j],dp[j-s[i]]+f[i]); } } } int ans=-inf; ;i<=;i++)
//dp[]的范围是100000~200000;i就是s[i],如果此时dp[i]也就是f[i]大于等于0的话,
//再加上s[i]-100000(界限)就是答案
; 
}

Cow Exhibition_背包(负数情况)的更多相关文章

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

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

  2. POJ 2184 01背包+负数处理

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10200   Accepted: 3977 D ...

  3. Margaritas on the River Walk_背包

    Description One of the more popular activities in San Antonio is to enjoy margaritas in the park alo ...

  4. Leetcode分类刷题答案&amp;心得

    Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...

  5. java 心得

    11. 最后的笑声 package javaBookPractice; public class LastLaugh { public static void main(String[] args) ...

  6. No.007:Reverse Integer

    问题: Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321 官方难度: Ea ...

  7. codevs2178 表达式运算Cuties[笛卡尔树]

    2178 表达式运算Cuties  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 大师 Master 题解  查看运行结果     题目描述 Description 给出一个表达 ...

  8. CodeForces 515A

    A. Drazil and Date time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  9. 《剑指offer》面试题11: 数值的整数次方

    面试题11: 数值的整数次方 剑指offer面试题11,题目如下 实现函数double power(double base,int exponent),求base的exponent次方, 不得使用库 ...

随机推荐

  1. C语言学习 第十一次作业总结

    作业总结 两次的作业,都是和指针有关.从第一次的作业开始,我就多次让同学们思考这个问题:为什么要用指针,为什么在函数的形参中要使用指针.如果能够想明白这2个问题,那么同学们应该会指针的了解就差不多足够 ...

  2. Cesium原理篇:3最长的一帧之地形(4:重采样)

           地形部分的原理介绍的差不多了,但之前还有一个刻意忽略的地方,就是地形的重采样.通俗的讲,如果当前Tile没有地形数据的话,则会从他父类的地形数据中取它所对应的四分之一的地形数据.打个比方 ...

  3. 2在HTML中使用JavaScript

    像HTML页面中插入JavaScrip的主要方法,就是使用<script>元素.HTML4.01为<script>定义了6个属性:async:可选,表示应该立即下载脚本,当不妨 ...

  4. DirectX API 编程起步 #01 项目设置

    =========================================================== 目录: DirectX API 编程起步 #02 窗口的诞生 DirectX A ...

  5. Linux修改oracle 10g的字符集

    修改数据库字符集为:ZHS16GBK查看服务器端字符集SQL > select * from V$NLS_PARAMETERS修改:$sqlplus /nologSQL>conn / as ...

  6. 宿主系统为Ubuntu 14,CentOS 6.5 安装VirtualBox增强工具失败:Building the OpenGL support module[FAILED]

    安装先前的笔记:CentOS 6.3 中安装VirtualBOX增强工具失败:Building the main Guest Additions module[FAILED],执行了以下命令 #安装 ...

  7. Git对象模型

    原文:http://gitbook.liuhui998.com/1_2.html 一.SHA 在git中,所有用来表示项目历史信息的文件,是通过一个40个字符的(40-digit)"对象名& ...

  8. Linux下/proc目录简介

    文章转载至:http://blog.csdn.net/zdwzzu2006/article/details/7747977 1. /proc目录Linux 内核提供了一种通过 /proc 文件系统,在 ...

  9. Android开发-开发前的配置

    一.JDK   (不用安装) JDK是 java 语言的软件开发工具,主要用于移动设备.嵌入式设备上的java应用程序. JDK是整个java开发的核心,它包含了JAVA的运行环境,JAVA工具和JA ...

  10. Linux下对于inode的理解

    0x01 什么是inode 文件存储在硬盘上,硬盘的最小存储单位叫做“扇区”(Sector),每个扇区储存512字节: 操作系统读取硬盘时,不会一个个扇区地读取,这样效率低,而是一次性连续读取多个扇区 ...