• <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 ) 閱讀(505) 評論(0)  編輯 收藏 引用 所屬分類: 解題報告
            日本欧美国产精品第一页久久| 噜噜噜色噜噜噜久久| 久久精品aⅴ无码中文字字幕不卡| 欧美一区二区三区久久综合| 精品无码久久久久国产| 久久精品国产影库免费看 | 久久久久久久久66精品片| 人妻少妇精品久久| 日日噜噜夜夜狠狠久久丁香五月 | 97久久婷婷五月综合色d啪蜜芽| 国内精品人妻无码久久久影院导航| 国内精品综合久久久40p| 久久91亚洲人成电影网站| 亚洲国产成人精品91久久久| 久久精品国产亚洲av影院| 日韩影院久久| 久久精品国产91久久综合麻豆自制 | 亚洲欧美精品伊人久久| 久久伊人五月丁香狠狠色| 青青青国产精品国产精品久久久久 | 狠狠色综合久久久久尤物| 久久久精品国产免大香伊| 亚洲天堂久久精品| 久久精品蜜芽亚洲国产AV| 2020久久精品亚洲热综合一本| 色综合久久久久网| 国产精品毛片久久久久久久 | 成人精品一区二区久久久| 人妻精品久久久久中文字幕一冢本| 日本亚洲色大成网站WWW久久| 久久精品无码一区二区三区| 久久婷婷五月综合色高清| 99久久这里只精品国产免费| 久久青青草原精品国产软件 | 精品国产乱码久久久久软件| 久久久精品久久久久特色影视| 久久99国产精品二区不卡| 2021精品国产综合久久| 91久久精品91久久性色| WWW婷婷AV久久久影片| 国产精品免费福利久久|