POJ 2184 Cow Exhibition Dairy Show (01 backpack, severe deformation)

Source: Internet
Author: User

Test instructions: There is a cow to prove that the cow is not stupid, so take some cow partners to prove themselves. Cows have an IQ and a sense of humor, both of which can be negative (difficult here), requiring that all cows ' IQ and/or sense of humor are not negative. The maximum value of the sum of the two.

Idea: Each cow can be brought or not on, is 01 knapsack problem. But the problem is that there is no obvious knapsack capacity limit, but there are some limitations that are not negative, the same is required maximum and. Must find a backpack capacity.

1) Backpack Capacity: can use the sum of humor or IQ and as a backpack capacity. Both are provided with a definite range.

2) The problem of the negative sign: cattle up to 100, and the IQ and sense of humor up to plus or minus 1000, then -1000*100<=x<=1000*100, can increase the range by 100000, then become 0<=x<=200000. This will satisfy the limit of capacity as a backpack.

3) Individual and cannot be negative: This restriction can filter out some mentally retarded cows (two properties are negative).

4) Sum: Sum =tf+ts. And TS has been recorded in DP [] As long as it is not negative. The TF is the subscript for the DP array, dp[100000] represents the sum that TF can get by 0 o'clock TS. DP[>100000] indicates that TF is positive, dp[<100000] indicates that TF is negative and negative is not considered. The positive solution is generated from the dp[>100000]. Is Tf+ts=j-100000+dp[j].

Note : How do I initialize a DP array? There's a bit of finesse here. In order to make the DP subscript is the TF sum, then the DP must not be all set 0 (otherwise dp[100010]=0, we have not done DP has been tf+ts=100010-100000-0=10, is not in line with the premise). But can be dp[100000] zero, ts+tf=0 very reasonable! And all the others are negative infinity. The reason is that we are in the calculation TS+TF have filtered out the single and negative, so when the DP is not started, all the dp[x]<0 are filtered out, only the dp[0]=0 left. furthermore , dp[j] to accurately represent the TF as J, there is a condition that "for a particular TF (i.e. J), Dp[j] must have meaning in order to carry out the Knapsack max". What do you mean? For example, our DP with a one-dimensional array, then generally from the back and forth, ans=max (ans, dp[j-iq]+ sense of humor), but when dp[j-IQ]= negative infinity, it is meaningless, that is, dp[j-IQ] does not have recorded the information of the cattle TS, can not be used to find Max. Therefore, it is necessary to add a judgment whether it is a negative infinity to determine whether it is meaningful, if not negative infinity, is meaningful (there is a record of some cattle TS information), you can ask Max. This allows Dp[tf]=ts to express the combination of TF and TS. If you follow the DP all 0 processing, can not determine whether TS is really 0, at the beginning of the sense of humor negative are not loaded into the backpack, even if the IQ of 1000 of the cattle. DP[J] Also can not be said that J is TF, when the first cow backpack, dp[100500]=dp[100499]=....=1000,1 said his sense of humor, so 100500+1000 express the tf+ts of this cow? No.

1#include <iostream>2#include <cstdio>3 using namespacestd;4 Const intn=200005;5 ints[ the],f[ the];6 intDp[n];7 intT;8 9 intCalintCnt1,intCnt2)Ten { One     intup=100000+cnt1;//capacity limit, avoid every sweep 20w times, if two cows why sweep 4W times?  A     intdown=100000+cnt2;//Lower Capacity -  -      for(intI=down; i<=up; i++) dp[i]=-N; thedp[100000]=0;//with a backpack capacity of 0 o'clock, the sense of humor is 0. Others must be initialized to negative.  -      -     //idea: Using intelligence as a backpack capacity, 100000 as 0, then the capacity of the range is around 100000 floating.  -      for(intI=0; i<t; i++) +     { -         if(s[i]<=0&&f[i]<=0)Continue;//If there is a s[i]+f[i]<0, may also need to take, for example, there are ( -5,2) and (1000,-4) two cows, when the first filter out, the second can not bring, two only with the maximum can be achieved. WA contributed in this.  +          A         if(s[i]>0) at         { -              for(intJ=up; j>=down+s[i]; j--)//from right to left -                 if(dp[j-s[i]]>-n)//Note: This cow must be brought in to add to its sense of humor, otherwise the i-100000 is not their intellect, but only the intellectual upper limit (useless value).  -DP[J] = max (dp[j-s[i]]+F[i], dp[j]); -         } -         Else in         { -              for(intJ=down; j<=up+s[i]; J + +) to                 if(dp[j-s[i]]>-N) +DP[J] = max (dp[j-s[i]]+F[i], dp[j]); -         } the     } *     intbig=-N; $      for(intI=100000; i<=up; i++)Panax Notoginseng         if(dp[i]>0) -Big=max (big, I-100000+dp[i]); the  +     if(big>0)returnBig; A     Else        return 0; the } +  - intMain () $ { $     //freopen ("Input.txt", "R", stdin); -      while(~SCANF ("%d",&t)) -     { the         intcnt1=0, cnt2=0;  -          for(intI=0; i<t; i++)Wuyi         { thescanf"%d%d",&s[i],&f[i]); -             if(s[i]>0) Wucnt1+=S[i]; -             Else Aboutcnt2+=S[i];  $         } -printf"%d\n", Cal (Cnt1,cnt2)); -     } -         return 0; A}
AC Code

POJ 2184 Cow Exhibition Dairy Show (01 backpack, severe deformation)

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.