Algorithm increases the maximum value

Source: Internet
Author: User

Problem DescriptionTo give n ordered integers to Ai bi, you need to select some integers to make all of the Ai+bi and max of the number you have selected. Also ask you to select the number of pairs of AI and non-negative, bi and non-negative.  Input format input The first behavior n, the number of pairs of the following n lines per line two integer AI bi output format output you selected pairs of Ai+bi and sample input 5-403-625-847 901-624-708-293 413886 709 Sample output 1715 data size and conventions 1<=n<=100-1000<=ai,bi<=1000 01 Backpack Variant, copy the other people's ideas: not directly calculate the Ai+bi of the selected number of and, but convert to calculate in AI and under certain circumstances try to make the selected bi and Max. So it became a problem similar to the 01 backpack. First filter out all AI and bi are less than 0 pairs, so that dp[i][j] represents the first I number of pairs, the selected AI and for the case of J, the maximum value of BI, will dp[i][j] initialized to-inf, and then initialize all known legal conditions: dp[i][a[i]] = B[i], DP[I][J] = max (Dp[i-1][j], dp[i][j]), if j-a[i] exists, dp[i][j] = max (Dp[i][j], Dp[i-1][j-a[i]] + b[i]). To avoid a negative number as an array subscript, add an offset.
Source: http://www.cnblogs.com/wangyiming/p/6476019.html
1#include <stdio.h>2 3 #defineMax (A, B) ((A&GT;B)? ( A):(B))4 #defineN 1000005 6 voidInputint[] ,int[] ,int );7 intMaxnum (int[] ,int[] ,int );8 9 intMainvoid)Ten { One     intN; Ascanf"%d", &n); -      -     intNa[n], nb[n]; the input (Na, Nb, n); -      -     intAns =Maxnum (Na, Nb, n); -printf"%d\n", ans >=0? Ans:0 );  +     return 0;  - } +  A voidInputintNa[],intNb[],intN) { at      while(N-- ){ -scanf"%d%d", Na + +, Nb + + ); -     } - } -  - intMaxnum (intNa[],intNb[],intN) { in     Static intdp[ -][n*2+1] = {0} ; -     intres =-N; to     intI, J; +      for(i =0; I < n; i + + ){ -          for(j =-N; j <= N; j + +) ){ theDp[i][j + N] =-N; *         } $     }Panax Notoginseng      -      for(i =0; I < n; i + + ){ theDp[i][n + na[i]] =Nb[i]; +     } A      the      for(i =1; I < n; i + + ){ +          for(j =-N; j <= N; j + +) ){ -Dp[i][j + N] = Max (Dp[i][j + n], dp[i-1][j +N]); $              $             if(j + N-na[i] >=0&& J + N-na[i] < n2 ){ -Dp[i][j + N] = Max (Dp[i][j + n], dp[i-1][j + N-na[i]] +nb[i]); -             } the         } -     }Wuyi      the      for(j =0; J <= N; J + + ){ -         if(dp[n-1][j + N] >=0 ){ Wures = Max (res, j + dp[n-1][j +N]); -         } About     } $      -     returnRes; -}

Algorithm increases the maximum value

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.