• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            雪竹的天空

            theorix

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              34 隨筆 :: 0 文章 :: 20 評論 :: 0 Trackbacks
            簡單的dp題
            注意輸出函數的編寫
            還有dp方程
              1/****************************************************************
              2    Problem: 1171
              3    User: theorix
              4    Language: C++
              5    Result: Accepted
              6    Time:152 ms
              7    Memory:980 kb
              8****************************************************************/

              9
             10#include<iostream>
             11#include<math.h>
             12using namespace std;
             13#define NN 5000
             14int ans[NN+9];
             15int mem[NN+9][2];
             16int xans[NN+9];
             17int xmem[NN+9][2];
             18int chose[NN+9];
             19void output2(int n)
             20{
             21    if(xmem[n][0]==0)
             22    {
             23        int i;
             24        for(i=1;i<=n;i++)
             25            printf("|");
             26        return ;
             27    }

             28    output2(xmem[n][0]);
             29    printf("x");
             30    output2(xmem[n][1]);
             31}

             32void output(int n)
             33{
             34    if(chose[n]==0)
             35    {
             36        for(int i=1;i<=n;i++)
             37            printf("|");
             38    }

             39    else if(chose[n]==1)
             40    {
             41        output(mem[n][0]);
             42        printf("+");
             43        output(mem[n][1]);
             44    }

             45    else if(chose[n]==2)
             46    {
             47        output2(mem[n][0]);
             48        printf("x");
             49        output2(mem[n][1]);
             50    }

             51}

             52int main()
             53{
             54//    freopen("toothpicks.in","r",stdin);
             55//    freopen("out.txt","w",stdout);
             56    int i,j,k,n,t,tt;
             57    xans[1]=1;
             58    for(i=2;i<=NN;i++)
             59    {
             60        t=(int)sqrt(i);
             61        xans[i]=i;
             62        for(j=2;j<=t;j++)
             63        {
             64            if(i%j==0&&xans[j]+xans[i/j]+2<xans[i])
             65            {
             66                xans[i]=xans[j]+xans[i/j]+2;
             67                xmem[i][0]=j;
             68                xmem[i][1]=i/j;
             69            }

             70        }

             71    }

             72    ans[1]=1;
             73    chose[1]=0;
             74    for(i=2;i<=NN;i++)
             75    {
             76        t=(int)sqrt(i);
             77        ans[i]=i;
             78        chose[i]=0;
             79        for(j=1;j<=i/2;j++)
             80        {
             81            if(ans[j]+ans[i-j]+2<ans[i])
             82            {
             83                ans[i]=ans[j]+ans[i-j]+2;
             84                mem[i][0]=j;
             85                mem[i][1]=i-j;
             86                chose[i]=1;
             87            }

             88        }

             89        for(j=2;j<=t;j++)
             90        {
             91            if(i%j==0&&xans[j]+xans[i/j]+2<=ans[i])
             92            {
             93                ans[i]=xans[j]+xans[i/j]+2;
             94                mem[i][0]=j;
             95                mem[i][1]=i/j;
             96                chose[i]=2;
             97            }

             98        }

             99    }

            100//for(i=1;i<=160;i++)cout<<i<<" "<<chose[i]<<endl;
            101//    for(n=1;n<=NN;n++)
            102    while(scanf("%d",&n)!=EOF)
            103    {//printf("%d\n",n);
            104        printf("%d toothpicks: ",ans[n]);
            105        output(n);
            106        printf("=%d\n",n);
            107    }

            108}

            109
            posted on 2008-09-06 21:04 雪竹的天空( theorix ) 閱讀(506) 評論(0)  編輯 收藏 引用 所屬分類: 解題報告
            99久久这里只精品国产免费| 久久被窝电影亚洲爽爽爽| 国产精品久久久久久一区二区三区 | 亚洲中文久久精品无码ww16| 久久精品人人做人人爽电影| 国产一区二区精品久久| 美女写真久久影院| 亚洲精品蜜桃久久久久久| 久久99精品国产99久久6男男| 欧美伊香蕉久久综合类网站| 色天使久久综合网天天| 国产精品国色综合久久| 亚洲伊人久久综合中文成人网| 久久99精品国产麻豆| 久久综合伊人77777| 久久亚洲国产午夜精品理论片| 日韩影院久久| 狠狠色丁香久久婷婷综| 久久久噜噜噜久久中文字幕色伊伊| 国产精品国色综合久久| 伊人久久大香线蕉综合Av| 久久久国产精华液| 久久成人永久免费播放| 国产一区二区三区久久| 久久久亚洲AV波多野结衣| 久久亚洲欧洲国产综合| 久久国产成人亚洲精品影院| 久久国产乱子伦精品免费强| 亚洲国产精品无码久久久蜜芽 | 久久久久亚洲爆乳少妇无| 国产精品九九九久久九九| 欧美黑人又粗又大久久久| 久久综合噜噜激激的五月天| 一本久久免费视频| 亚洲国产精品综合久久一线| 香蕉久久永久视频| 免费精品久久久久久中文字幕| 久久久久亚洲AV综合波多野结衣 | 久久综合狠狠色综合伊人| 精品无码久久久久国产| 久久这里只有精品18|