poj1837 (Balance)

Source: Internet
Author: User

Title Address: Balance

Main topic:

There is a balance, there are several hooks on both sides of the balance, a total of C hooks, there is a G hook code, the hook code all hanging to the hook so that the balance of the total number of methods. The Libra can be viewed as a horizontal axis with an x-axis of 0 points as the balance point.

Problem Solving Ideas:

DP: The most extreme balance is that all objects hang at the far end, so the maximum balance is j=15*20*25=7500. In principle there should be dp[1~20][-7500 ~ 7500]. Converted to 01 backpack, where I represents the first I hook to reach the J-balance of all cases, because it is a balance so the balance of the middle value is the result of the balance, because there is a negative DP conversion to dp[1~20][0 ~ 16000]. DP[G][8000] can, remember initialization, initialization can only be initial dp[0][8000], if all hooks to 0 of the balance of the initial impact on the results.

Can be set Dp[i-1][j]=num, then for DP[I][J] There are two kinds of decision, is to choose where the first weight of the position or not put. So Dp[i][j]+=dp[i-1][j-pos[k]*weight[i]]. Of course the precondition is j>=pos[k]*weight[i].

Code:

1#include <algorithm>2#include <iostream>3#include <sstream>4#include <cstdlib>5#include <cstring>6#include <cstdio>7#include <string>8#include <bitset>9#include <vector>Ten#include <queue> One#include <stack> A#include <cmath> -#include <list> -#include <map> the#include <Set> - using namespaceStd/***************************************/ - #definell Long Long - #defineInt64 __int64/***************************************/ + Const intINF =0x7f7f7f7f; - Constll linf = (1ll<< -); + Const DoubleEPS = 1e-8; A Const DoublePie=acos (-1.0); at Const intd1x[]= {0,-1,0,1}; - Const intd1y[]= {-1,0,1,0}; - Const intd2x[]= {0,-1,0,1}; - Const intd2y[]= {1,0,-1,0}; - Const intfx[]= {-1,-1,-1,0,0,1,1,1}; - Const intfy[]= {-1,0,1,-1,1,-1,0,1}; inInlineintMin_32 (int(a),int(b)) {return(a) < (b)?(a):(B); -InlineintMax_32 (int(a),int(b)) {return(a) > (b)?(a):(B); toInlineLong LongMin_64 (Long Long(a),Long Long(b)) {return(a) < (b)?(a):(B); +InlineLong LongMax_64 (Long Long(a),Long Long(b)) {return(a) > (b)?(a):(B); - /***************************************/ the voidOpenFile () { *Freopen ("data.in","RB", stdin); $Freopen ("Data.out","WB", stdout);Panax Notoginseng } - /********************** Gorgeous split line, above for template part *****************/ the  + intdp[ +][20000];//status of the balance on behalf of the I hook A intc[ +]; the intg[ +]; + intMain () - { $     intc,g; $      while(SCANF ("%d%d", &c,&g)! =EOF) -     { -Memset (c,0,sizeof(c)); theMemset (G,0,sizeof(g)); -Memset (DP,0,sizeof(DP));Wuyi         inti,j,k; the          for(i=1; i<=c;i++) -scanf"%d",&c[i]); Wu          for(i=1; i<=g;i++) -scanf"%d",&g[i]); About        //For (i=0;i<=16000;i++) $dp[0][8000]=1;//Initialize -          for(i=1; i<=g;i++) -              for(j=0; j<=16000; j + +) -                  for(k=1; k<=c;k++) A                     if(j>=c[k]*G[i]) +dp[i][j]+=dp[i-1][j-c[k]*G[i]]; theprintf"%d\n", dp[g][8000]); -     } $     return 0; the}
View Code

poj1837 (Balance)

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.