poj 1837 天平Balance

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   div   

http://poj.org/problem?id=1837

題意:現有一個天平,可以視為x座標軸,負半軸為天平左邊,右半軸為天平右邊,

        現在天平上有c個掛鈎,讓你掛上g個的砝碼,問有幾種掛法使得天平平衡,

        2 <= C <= 20 ;   2 <= G <= 20  ;  

       掛鈎座標[-15,15]  ;   砝碼重量[1,25]  ;

 思路:試想,每次掛上一個砝碼   天平的平衡度  是受  前一個砝碼的位置  的影響   故   可以考慮用動態規劃

               可不可以把 第一個 砝碼掛在不同的位置  所達到的平衡度  j  給記錄下來   

               在已掛上   第一個砝碼的所達到  平衡度為j  的基礎上   再  掛第二個砝碼    達到  平衡度  j‘     記錄下來   

      

         故:用  dp[i][k]  來表示   掛上  i   個砝碼後  達到平衡度  j  的掛法個數  (平衡度j為0時表示天平平衡)

        《

               根據題意平衡度j最大時  是  把所有砝碼掛在天平的一側的最外的鉤上   15*20*25  = 7500

               原則上就應該有dp[ 1~20 ][-7500 ~ 7500 ]

               但是下標不能為負  故  處理一下   dp[1~20][0~15000]    處理後   j為7500時  表示平衡

         》 

         顯然的   dp[0][7500]=1    是已知的

         因此  dp[g][7500]  的值   就是要 求的答案

 

         如何求: dp[ i ][ k+w[i]*l[j] ]  +=  dp[ i-1 ][k] 

                      w[i]表示第i個砝碼的重量w[i]

                      l[j]表示第i個砝碼所在的位置l[j]

 

        

  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> 10 #include <queue> 11 #include <stack> 12 #include <cmath> 13 #include <list> 14 #include <map> 15 #include <set> 16 using namespace std; 17 /*10^8-----1s*/ 18 /***************************************/ 19 typedef vector<int> VI; 20 typedef vector<char> VC; 21 typedef vector<string> VS; 22 typedef set<int> SI; 23 typedef set<string> SS; 24 typedef map<int ,int> MII; 25 typedef map<string,int> MSI; 26 typedef pair<int,int> PII; 27 typedef vector<PII> VII; 28 typedef vector<VI > VVI; 29 /***************************************/ 30 #define min(a,b) (a>b?b:a) 31 #define max(a,b) (a>b?a:b) 32  33 #define clr(a,b) memset(a,b,sizeof(a)) 34 #define all(x)    (x).begin(), (x).end() 35 #define sz(x) ((int)(x).size()) 36 #define ll long long 37 #define int64 __int64 38 #define pb push_back 39 #define mp make_pair 40 #define LL(x) ((x)<<1) 41 #define RR(x) ((x)<<1|1) 42 #define ri(x) scanf("%d",&x) 43 #define rii(x,y) scanf("%d%d",&x,&y) 44 #define rd(x) scanf("%lf",&x) 45 #define rdd(x,y) scanf("%lf%lf",&x,&y) 46 #define rs(x) scanf("%s",x) 47 #define pi(x) printf("%d",x) 48 #define pin(x) printf("%d\n",x) 49 #define ps(x) printf("%s",x) 50 #define pn()  printf("\n") 51 #define sqr(x) ((x)*(x)) 52 #define rep(i,a,b)  for(int i=(a);i<(b);i++) 53 #define repu(i,a,b) for(int i=(a);i<=(b);i++) 54 #define repd(i,a,b) for(int i=(a);i>=(b);i--) 55 #define repc(i,a,c) for(int i=(a);(c);i++) 56 /***************************************/ 57 const int INF = 0x7f7f7f7f; 58 const double eps = 1e-8; 59 const double PIE=acos(-1.0); 60 const int dx[]= {0,-1,0,1}; 61 const int dy[]= {1,0,-1,0}; 62 const int fx[]= {-1,-1,-1,0,0,1,1,1}; 63 const int fy[]= {-1,0,1,-1,1,-1,0,1}; 64 /***************************************/ 65 void openfile() 66 { 67     freopen("data.in","rb",stdin); 68     freopen("data.out","wb",stdout); 69 } 70 /**********************The End OF The Template*****************/ 71  72 /* 73  74  75 */ 76  77 int w[30];//w[i]表示第i個砝碼的重量w[i];  i=[2~20]   w[i]=[1~25] 78 int l[21];//l[j]表示第i個砝碼所在的位置l[j]; l[j]=[-15~15]   j=[2~20] 79 int dp[30][15002];//dp[i][k] 表示掛了i個砝碼,平衡度為k的掛的種數    k=[-15*25*20,15*25*20] 80 //初始dp[0][7500]=1 81 int main() 82 { 83     int  c,g; 84     int i,j,k; 85     scanf("%d %d",&c,&g); 86     for(i=0; i<c; i++) 87     { 88         scanf("%d",&l[i]); 89     } 90     for(i=0; i<g; i++) 91     { 92         scanf("%d",&w[i]); 93     } 94     memset(dp,0,sizeof(dp)); 95     dp[0][0+7500]=1;//天平在不掛鈎碼時,達到平衡,方法為1種 96     for(i=1; i<=g; i++) //天平上砝碼的個數 97         for(k=0; k<=15000; k++)  // 平衡度k 98         { 99             if(dp[i-1][k])100                 for(j=0; j<c; j++)101                     dp[i][k+w[i-1]*l[j]]+=dp[i-1][k];102         }103     printf("%d\n",dp[g][7500]);104     return 0;105 }
View Code

 

                 

      

   

poj 1837 天平Balance

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.