POJ 2184 Cow Exhibition (variant 01 backpack)

Source: Internet
Author: User

Test instructions: There are some cows, they have a certain S value and f value, these values have a positive negative, and finally let the guaranteed s and for non-negative and F, and for non-negative cases, the maximum value of s+f.
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

#include <stdio.h>#include <string.h>#include <algorithm>using namespace STD;intdp[200005];Const intINF =1<< -;intMain () {intn,s[ $],f[ $],i,j,ans; while(~scanf("%d", &n)) { for(i =0; i<=200000;        i++) Dp[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)Continue;if(s[i]>0)            { for(j =200000; j>=s[i]; j--)//If s[i] is an integer, then we 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]); }Else{ for(j = s[i]; j<=200000+s[i]; J + +)//For negative numbers you need to reverse                    if(dp[j-s[i]]>-inf) Dp[j] = max (dp[j],dp[j-s[i]]+f[i]); }} ans =-inf; for(i =100000; i<=200000; i++) {if(dp[i]>=0) ans = max (ans,dp[i]+i-100000); }printf("%d\n", ans); }return 0;}

POJ 2184 Cow Exhibition (variant 01 backpack)

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.