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. [转]设计一款Android App总结

    开发工具的选择 开发工具我将选用Android Studio,它是Google官方指定的Android开发工具,目前是1.2.2稳定版,1.3的预览版也已经发布了.Android Studio的优点就 ...

  2. &lt;input type=&quot;file&quot;&gt;火狐兼容

    <input type="file">放着a标签下火狐不兼容 <a href=""><input type="file& ...

  3. Java多线程系列--“JUC锁”07之 LockSupport

    概述 本章介绍JUC(java.util.concurrent)包中的LockSupport.内容包括:LockSupport介绍LockSupport函数列表LockSupport参考代码(基于JD ...

  4. HDOJ 4336 Card Collector

    容斥原理+状压 Card Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  5. Eclipse无法启动报An internal error occurred during: &quot;reload maven project&quot;. java.lang.NullPointerException

    由于没有正常关机导致eclipse无法将数据正常写入配置文件导致无法启动.报这样一个异常 An internal error occurred during: "reload maven p ...

  6. Android nDrawer

    GitHub上一款流行的侧滑,附上自己as编译过的源码http://download.csdn.net/detail/lj419855402/8559039. 留个纪念,说不定以后用得到. 依赖一个l ...

  7. yebis error ---depth of field

    前几天在墙外无法登陆cnblogs...导致很多blogs就没写了 有几篇比较值得记下来的,但是我已经不记得了,应该和sao有关scalable ambient obscurance 我似乎回忆起一点 ...

  8. github Top100

    nodejs 文件 var restify = require('restify'), moment = require('moment'), fs = require('fs'), yesterda ...

  9. Bridge 桥梁模式 桥接模式

    简介 将[抽象部分](Abstraction,人)与[实现部分](Implementor,人穿的衣服)分离,使它们都可以独立的变化. [业务抽象角色]引用[业务实现角色],或者说[业务抽象角色]的部分 ...

  10. DLL入门浅析(5)——使用DLL在进程间共享数据

    转载自:http://www.cppblog.com/suiaiguo/archive/2009/07/21/90734.html 在Win16环境中,DLL的全局数据对每个载入它的进程来说都是相同的 ...