POJ 2184 Cow Exhibition (DP 01 backpack variant)

Source: Internet
Author: User

Description

"Fat and docile, big and dumb, they look so stupid, they aren ' t muchFun ..."-cows with guns by Dana Lyons the cows want to prove to the PublicThat they is both smart and fun. In order to Do  This, Bessie have organized an exhibition that would be put on by the cows. She has given each of the N (1<= N <= -) Cows a thorough interview and determined the values forEach cow:the smartness Si (- +<= Si <= +) of the cow and the funness Fi (- +<= Fi <= +) of the cow. Bessie must choose which cows she wants to bring to her exhibition. She believes the total smartness TS of the group isThe sum of the Si's and, likewise, the total funness TF of the group are 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 mu St also show that the cows is 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

12.. n+1 for

Output


1 0

Sample Input

5-578 -66 -321-8 -5

Sample Output

8

Hint

OUTPUT Details:bessie chooses cows1,3, and4, giving values of TS =-5+6+2=3and TF=7-3+1=5, so3+5=8. Note that adding cow2would improve the value of TS+TF toTen, but theNewValue of TF would is negative, so it isNot allowed.

Source

Usaco 2003 Fall
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6#include <stdlib.h>7 using namespacestd;8 #defineINF 1<<309 #defineN 106Ten intdp[200006]; One intA[n],b[n]; A intMain () - { -     intN; the      while(SCANF ("%d", &n) = =1){ -          for(intI=0; i<n;i++){ -scanf"%d%d",&a[i],&b[i]); -         } +         //memset (Dp,inf,sizeof (DP)); -          for(intI=0;i<200006; i++){ +dp[i]=-inf; A         } atdp[100000]=0; -          for(intI=0; i<n;i++){ -             //if (a[i]<0 && b[i]<0) continue; -             if(a[i]>0){ -                  for(intj=200000; j>=a[i];j--){ -Dp[j]=max (dp[j],dp[j-a[i]]+b[i]); in                 } -             } to             Else{ +                  for(intj=a[i];j<=200000+a[i];j++){ -Dp[j]=max (dp[j],dp[j-a[i]]+b[i]); the                 } *             } $         }Panax Notoginseng         intans=0; -          for(intI=100000; i<=200000; i++){ the             if(dp[i]>=0){ +Ans=max (ans,dp[i]+i-100000); A             } the         } +printf"%d\n", ans); -     } $     return 0; $}
View Code Test instructions: Each line gives Si and fi, representing two properties of the cow, and then asked to select a few cows, yes, the sum of the total S and Total F, note s and F are not negative idea: It is obvious that the problem is taken and not taken, the first idea of this kind of problem is the backpack, but this topic is obviously different from the general backpack, because there are negative, but Lenovo to the former also have this negative deposit in the subscript, that is, the array opened large, in exchange for a kind of deposit method we use Dp[i] to store each s[i] can get the best f, then we can take the value of the s[i] for two different 01-pack, after the take, and then according to the backpack to find out the best answer can be Attach the Great God code:
1#include <stdio.h>2#include <string.h>3#include <algorithm>4 using namespacestd;5 6 intdp[200005];7 Const intINF =1<< -;8 9 intMain ()Ten { One     intn,s[ $],f[ $],i,j,ans; A      while(~SCANF ("%d",&N)) -     { -          for(i =0; i<=200000; i++) theDp[i] =-inf; -dp[100000] =0; -          for(i =1; i<=n; i++) -scanf"%d%d",&s[i],&f[i]); +          for(i =1; i<=n; i++) -         { +             if(s[i]<0&& f[i]<0) A                 Continue; at             if(s[i]>0) -             { -                  for(j =200000; j>=s[i]; j--)//if S[i] is an integer, then we will carry the backpack from a large, small direction. -                     if(dp[j-s[i]]>-inf) -DP[J] = max (dp[j],dp[j-s[i]]+f[i]); -             } in             Else -             { to                  for(j = s[i]; j<=200000+s[i]; J + +)//is negative, you need to reverse +                     if(dp[j-s[i]]>-inf) -DP[J] = max (dp[j],dp[j-s[i]]+f[i]); the             } *         } $Ans =-inf;Panax Notoginseng          for(i =100000; i<=200000; i++)//because the interval 100000~200000 is the integer represented, then I is the s[i in the previous backpack], if this time dp[i] is f[i] greater than or equal to 0, we add s[i] (this time is I), and then subtract as the bounds of 100000, You'll get the answer . -         { the             if(dp[i]>=0) +ans = max (ans,dp[i]+i-100000); A         } theprintf"%d\n", ans); +     } -  $     return 0; $}
View Code

POJ 2184 Cow Exhibition (DP 01 backpack variant)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.