zoukankan      html  css  js  c++  java
  • POJ 1837 Balance 【DP】

    题意:给出一个天平,给出c个钩子,及c个钩子的位置pos[i],给出g个砝码,g个砝码的质量w[i],问当挂上所有的砝码的时候,使得天平平衡的方案数,

    用dp[i][j]表示挂了前i个砝码时,平衡点为j时的总的方案数,

    状态转移为第i个砝码是否挂上,如果要挂上第i个砝码的话,j>=pos[i]*w[i](力矩=力臂*力) 因为最大的力矩为 20*15*25=150000

     1 #include<iostream>  
     2 #include<cstdio>  
     3 #include<cstring>  
     4 #include<algorithm>  
     5 using namespace std;
     6 
     7 int dp[25][15000],pos[25],w[25];
     8 
     9 int main()
    10 {
    11     int i,j,k,c,g;
    12     while(scanf("%d %d",&c,&g)!=EOF)
    13     {
    14         for(i=1;i<=c;i++) scanf("%d",&pos[i]);//c个钩子,每个钩子的位置 
    15         for(i=1;i<=g;i++) scanf("%d",&w[i]);//g个砝码,每个砝码的质量 
    16         memset(dp,0,sizeof(dp));
    17         dp[0][7500]=1;//两边不挂砝码的时候,平衡的方案数为1 
    18         
    19         for(i=1;i<=g;i++)
    20         {
    21             for(j=0;j<=15000;j++)
    22             {
    23                 for(k=1;k<=c;k++)
    24                 {
    25                     if(j>=pos[k]*w[i])
    26                     dp[i][j]+=dp[i-1][j-pos[k]*w[i]];
    27                 }
    28             }
    29         }
    30         printf("%d
    ",dp[g][7500]);
    31     }
    32     return 0;
    33 }
    View Code

    其实觉得这一题似懂非懂= =看着题解,看着代码,觉得是这么回事,可是再一细想,为什么能够这么做呢,j的值太大了,打印看中间的值看不了= =

    再想一想= =

    go--go--

  • 相关阅读:
    《父亲写的散文诗》--许飞
    python 解数独
    github key already in use
    openwrt ddns绑定域名
    hexo 长期后台运行
    修复云服务器rpm无法使用的问题
    vim 取消筛选高亮
    力扣 2021.02.25 最长公共前缀
    [模板]-Manacher
    背包问题回顾
  • 原文地址:https://www.cnblogs.com/wuyuewoniu/p/4302676.html
Copyright © 2011-2022 走看看