青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

QuXiao

每天進步一點點!

  C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
  50 隨筆 :: 0 文章 :: 27 評論 :: 0 Trackbacks

解題報告


題目來源:

PKU 1037 A decorative fence

分類:

DP

原文:

A decorative fence

 

Time Limit: 1000MS


Memory Limit: 10000K

Total Submissions: 2051


Accepted: 608

Description

Richard just finished building his new house. Now the only thing the house misses is a cute little wooden fence. He had no idea how to make a wooden fence, so he decided to order one. Somehow he got his hands on the ACME Fence Catalogue 2002, the ultimate resource on cute little wooden fences. After reading its preface he already knew, what makes a little wooden fence cute.
A wooden fence consists of N wooden planks, placed vertically in a row next to each other. A fence looks cute if and only if the following conditions are met:
?The planks have different lengths, namely 1, 2, . . . , N plank length units.
?Each plank with two neighbors is either larger than each of its neighbors or smaller than each of them. (Note that this makes the top of the fence alternately rise and fall.)
It follows, that we may uniquely describe each cute fence with N planks as a permutation a1, . . . , aN of the numbers 1, . . . ,N such that (any i; 1 < i < N) (ai − ai−1)*(ai − ai+1) > 0 and vice versa, each such permutation describes a cute fence.
It is obvious, that there are many di
erent cute wooden fences made of N planks. To bring some order into their catalogue, the sales manager of ACME decided to order them in the following way: Fence A (represented by the permutation a1, . . . , aN) is in the catalogue before fence B (represented by b1, . . . , bN) if and only if there exists such i, that (any j < i) aj = bj and (ai < bi). (Also to decide, which of the two fences is earlier in the catalogue, take their corresponding permutations, find the first place on which they differ and compare the values on this place.) All the cute fences with N planks are numbered (starting from 1) in the order they appear in the catalogue. This number is called their catalogue number.


After carefully examining all the cute little wooden fences, Richard decided to order some of them. For each of them he noted the number of its planks and its catalogue number. Later, as he met his friends, he wanted to show them the fences he ordered, but he lost the catalogue somewhere. The only thing he has got are his notes. Please help him find out, how will his fences look like.

Input

The first line of the input file contains the number K (1 <= K <= 100) of input data sets. K lines follow, each of them describes one input data set.
Each of the following K lines contains two integers N and C (1 <= N <= 20), separated by a space. N is the number of planks in the fence, C is the catalogue number of the fence.
You may assume, that the total number of cute little wooden fences with 20 planks fits into a 64-bit signed integer variable (long long in C/C++, int64 in FreePascal). You may also assume that the input is correct, in particular that C is at least 1 and it doesn
exceed the number of cute fences with N planks.

Output

For each input data set output one line, describing the C-th fence with N planks in the catalogue. More precisely, if the fence is described by the permutation a1, . . . , aN, then the corresponding line of the output file should contain the numbers ai (in the correct order), separated by single spaces.

Sample Input

2

2 1

3 3

Sample Output

1 2

2 3 1

Source

CEOI 2002

 

 

中文描述:

                首先告訴你,如果一個由n個木條構成的柵欄是美觀的,那么它必須具備兩個條件:1、n個木條必須具有不同的長度;2、與任一木條相鄰的兩個木條,它們的高度既不會都比中間的木條高,也不會都比中間的木條低。(也就是說柵欄呈“波浪”型排列)由n個木條構成的柵欄,可以有多種排列方式。為了區分這些排列方式,還定義了排列方式之間的大小關系。假設一種排列方式為aa1,a2,a3…an),另一種排列方式為bb1,b2,b3…bn),a<b當且僅當存在i使得,對于任意j<iaj=bj,且ai<bi。(其實就是字典順序。)現在給你木條數nc,n個木條構成的柵欄,排列順序從小到大排列,問你第c中排列的順序是什么。

題目分析與算法模型

                通過分析可以知道,木條的具體長度是多少并不重要,只要滿足木條之間的長度不相等就可以了。為了簡單起見,就設n根木條的長度從小到大依次為1n。先從n比較小的情況找找規律,比如n=4,那么排列順序為:1 3 2 41 4 2 3、2 1 4 3、2 3 1 42 4 1 3、3 1 4 2、3 2 4 1、3 4 1 24 1 3 2、4 2 3 1??梢钥闯?,當第一位數字X已經確定時,X后面的數字一定是先從1X-1開始考慮(比X低的那些木條),然后再從X+1n考慮(比X高的那些木條)。其實,我們可以把排列方式分為兩種,一種是第一個木條比第二個木條高的排列方式,我們簡記為W方式;另一種是第一個木條比第二個木條低的排列方式,我們簡記為M方式。當第一個數字確定時,我們總是首先考慮所有W方式,等W方式全部考慮完,再去考慮所有的M方式。那么,n個木條的排列順序就為:

W1         (以第一根木條開始的W排列方式)

M1         (以第一根木條開始的M排列方式)

W2        

M2

Wn

Mn

                假設我們可以知道每一種排列方式的種類,那么我們就可以判斷出我們要求的第c種排列方式落在了上述的哪個區間,這樣我們就可以把第一根木條確定下來。規模較小到了1n-1,若木條落在W方式下,我們就找下一個的M方式;若木條是落下M方式,我們就找下一個的W方式,用同樣的方法確定第二根木條,然后以此類推,求出所有的木條,問題就可以解決了!但是別急,貌似其中還有一些問題。當我們確定了一根木條,去找第二根木條時,剩下的木條的長度就是不連續的了,子問題與原問題是同一類問題嗎?其實不用擔心,原問題是1n個長度不同的木條以M或者W方式排列的個數,我們只是為了方便記錄狀態,而將他們的長度規定為1n,即使它們的長度是不連續的,問題的本質還是一樣的。我們可以這樣想:我們在第一步確定了1n個木條中的第x個,那么我們在尋找下一根木條時,事先可以將(x+1)~n的木條長度減1,這樣長度就變成了長度為1~(n-1)的n-1根木條的排列問題了。

           我們可以設M[x][n]為由n個長度不同的木條組成的柵欄中,以其中第x根木條開始的“M”型排列的個數,W[x][n]為由n個長度不同的木條組成的柵欄中,以其中第x根木條開始的“W”型排列的個數??梢酝瞥鲆韵鹿剑?/span>

                M[x][n] = ∑W[i][n-1]      (1<=i<=x-1)

                W[x][n] = ∑M[i][n-1]      (x<=i<=n-1)

                M[x][n] = W[n-x+1][n]  (長度從小到大排列的木條,第x根與第n-x+1根是對應的)

                這樣確定了狀態和狀態轉移,只要事先進行預處理,計算出WM,就可以一步步推算出區間,最終算出第c個排列的情況。這里要注意:在查找第一根木條時,既要考慮W方式,又要考慮M方式。而在已找到是在W(或M)方式的區間的時候,就只能考慮M(或W)方式了。

               

代碼:


#include <iostream>
using namespace std;
 
const int MAX = 21;
 
__int64 W[MAX][MAX];
__int64 M[MAX][MAX];
int used[MAX];         //用于輸出時判斷木條的使用情況
__int64 c;
int n;
 
void Input ()
{
        scanf("%d%I64d", &n, &c);
}
 
//利用遞歸進行初始化
__int64 getDown (int x, int n);
__int64 getUp (int x, int n);
 
__int64 getDown (int x, int n)
{
        if ( W[x][n] != -1 )
               return W[x][n];
        int i;
        __int64 ans;
        ans = 0;
        for (i=1; i<x; i++)
               ans += getUp (i, n-1);
 
        return ( W[x][n] = ans );
}
 
__int64 getUp (int x, int n)
{
        if ( M[x][n] != -1 )
               return M[x][n];
 
        return ( M[x][n] = getDown(n-x+1, n) );
}
 
void Init ()
{
        int i, j;
 
        memset(W, -1, sizeof(W));
        memset(M, -1, sizeof(M));
 
        for (i=1; i<=MAX; i++)
               W[1][i] = 0;
        W[1][1] = 1;
        M[1][1] = 1;
        W[1][2] = 0;
        M[1][2] = 1;
        W[2][2] = 1;
        M[2][2] = 0;
 
        for (i=3; i<=MAX; i++)
        {
               for (j=1; j<=i; j++)
               {
                       getDown(j, i);
                       getUp(j, i);
               }
        }
 
}
 
//子問題中,1k范圍內第x根木條實際的位置
void Output (int x, int k)
{       
        int i;
        do
        {
               for (i=1; i<=k; i++)
               {
                        //1k中有以前已確定的木條,并且x木條比已確定的木
                        //條長度要長,那么x木條的長度應加1
                       if ( used[i] && x >= i )
                       {
                               x++;
                               k++;
                       }
               }
        } while ( used[x] );
        if ( k > 1 )
               printf("%d ", x);
        else
               printf("%d", x);
        used[x] = 1;
        
}
 
void Solve ()
{
        int up, i, first;
        up = 1;
        i = 1;
        first = 1;
        memset(used, 0, sizeof(used));
 
        while ( n > 0 )
        {
               if ( up == 1 )
               {
                       if ( c > M[i][n] )
                       {
                               if ( first )   //是否是在查找第1根木條
                               {
                                      up = !up;
                               }
                               c -= M[i][n];
                               i++;
                       }
                       else
                       {
                               Output(i, n);
                               first = 0;
                               //i = 1;
                               up = !up;
                               n--;
                       }
               }
               else
               {
                       if ( c > W[i][n] )
                       {
                               if ( first )
                               {
                                       up = !up;
                               }
                               c -= W[i][n];
                               if ( !first )
                               {
                                      i ++;
                               }
                       }
                       else
                       {
                               Output(i, n);
                               first = 0;
                               up = !up;
                               n--;
                               i = 1;
                       }
               }
        }
 
        printf("\n");
}
 
int main ()
{
        int test;
        cin>>test;
        Init ();
        while ( test-- )
        {
               Input ();
               Solve ();
        }
 
        return 0;
}

 

心得:

                當預感到一道題目可以用DP做時,不妨先想出一個可以表示問題的狀態,再看是否可以根據題中的所描述的操作進行狀態轉移,并且看轉移之后的狀態是否是假設出的狀態,即看自己假設出的表示問題的狀態是否具有最優子結構。若沒有,則應該假設另一種狀態或者在原先狀態的基礎上進行修改和加強,直到找出符合最優子結構的狀態。

posted on 2008-05-20 12:45 quxiao 閱讀(2153) 評論(3)  編輯 收藏 引用 所屬分類: ACM

評論

# re: PKU 1037 A decorative fence 2008-07-29 13:36 STARTING
很久不見你更新了阿~~  回復  更多評論
  

# re: PKU 1037 A decorative fence 2008-07-30 14:17 quxiao
@STARTING
最近也在寫解題報告,就是忘記貼上去了 :P  回復  更多評論
  

# re: PKU 1037 A decorative fence 2009-04-24 20:01 Las vagas
“我們可以這樣想:我們在第一步確定了1~n個木條中的第x個,那么我們在尋找下一根木條時,事先可以將(x+1)~n的木條長度減1,這樣長度就變成了長度為1~(n-1)的n-1根木條的排列問題了?!苯浀洌。?!  回復  更多評論
  

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            欧美日韩1区2区3区| 亚洲天堂免费观看| 午夜一级久久| 欧美日韩国产在线一区| 一区视频在线播放| 欧美一区午夜视频在线观看| 在线综合视频| 另类酷文…触手系列精品集v1小说| 国产尤物精品| 亚洲制服av| 亚洲一区二区三区在线播放| 欧美成人一区二区三区在线观看| 亚洲福利国产| 亚洲二区在线视频| 欧美激情视频网站| 99riav1国产精品视频| 亚洲日本在线观看| 欧美精选一区| 亚洲专区在线视频| 亚洲伊人观看| 国内久久精品| 欧美va亚洲va香蕉在线| 麻豆成人精品| 一本久道综合久久精品| 99国产精品久久| 国产精品一区二区三区观看 | 亚洲欧美资源在线| 欧美日韩国产一级| 中文精品99久久国产香蕉| 亚洲精品国产精品国自产观看| 久久久久久午夜| 亚洲日本中文字幕免费在线不卡| 亚洲精品美女在线观看播放| 欧美性jizz18性欧美| 久久高清国产| 免费在线视频一区| 亚洲女同精品视频| 久久岛国电影| 一本久久综合亚洲鲁鲁五月天| 妖精视频成人观看www| 国产精品入口夜色视频大尺度 | 亚洲欧美大片| 在线精品在线| 这里只有精品丝袜| 在线观看精品| 99精品热视频只有精品10| 国产一本一道久久香蕉| 欧美承认网站| 国产精品免费网站在线观看| 免费影视亚洲| 欧美性大战久久久久| 久久综合色88| 国产精品成人观看视频免费| 六月婷婷一区| 国产精品久久久久久久久借妻| 鲁大师成人一区二区三区| 欧美日韩国产成人在线91| 久久男人资源视频| 欧美日韩一区二区欧美激情| 麻豆9191精品国产| 国产精品分类| 亚洲精品国产精品久久清纯直播| 国产亚洲成av人在线观看导航 | 欧美国产大片| 久久久亚洲欧洲日产国码αv| 欧美精品一区在线| 裸体歌舞表演一区二区| 国产精品天天看| 亚洲精品小视频| 在线观看视频一区二区| 亚洲免费在线看| 亚洲女性裸体视频| 欧美激情综合五月色丁香小说 | 激情视频一区| 亚洲欧美日韩精品久久久久| 亚洲小视频在线观看| 免费精品视频| 欧美暴力喷水在线| 精品成人一区二区三区四区| 欧美在线电影| 久久精品国产一区二区三| 国产精品地址| 中文一区二区在线观看| 一本色道久久88综合日韩精品| 欧美91视频| 亚洲成在人线av| 亚洲国产精品欧美一二99| 久久国产视频网站| 久久久久国产一区二区三区四区| 国产精品白丝av嫩草影院| 99精品99| 欧美一站二站| 国产精品羞羞答答xxdd| 亚洲综合电影一区二区三区| 欧美一级久久| 欧美电影专区| 一本久久a久久精品亚洲| 亚洲深夜福利| 国产精品高潮粉嫩av| 亚洲午夜在线观看| 欧美在线亚洲综合一区| 黄色工厂这里只有精品| 美女视频黄a大片欧美| 亚洲国产裸拍裸体视频在线观看乱了| 亚洲人成网站精品片在线观看| 欧美mv日韩mv国产网站| 99视频热这里只有精品免费| 午夜激情久久久| 国产在线欧美日韩| 欧美黄在线观看| 亚洲视频一区二区| 久色成人在线| 99精品国产福利在线观看免费| 欧美天天在线| 久久精品人人做人人综合| 久久噜噜亚洲综合| 日韩特黄影片| 欧美午夜不卡| 久久综合导航| 中文国产亚洲喷潮| 免费欧美在线视频| 99精品国产99久久久久久福利| 国产欧美日韩在线视频| 欧美成人性网| 欧美一区二视频在线免费观看| 亚洲电影免费| 久久久久国产精品麻豆ai换脸| 亚洲精品中文在线| 国产欧美精品一区aⅴ影院| 欧美11—12娇小xxxx| 亚洲欧美成人一区二区在线电影| 欧美好吊妞视频| 欧美专区第一页| 亚洲最新在线视频| 经典三级久久| 国产精品久久久一本精品| 欧美a级在线| 久久国产精彩视频| 亚洲天堂久久| 亚洲美女色禁图| 欧美激情按摩在线| 久久久久9999亚洲精品| 亚洲综合导航| 中文精品视频一区二区在线观看| 精品成人久久| 国内精品国语自产拍在线观看| 欧美日韩精品免费观看视频| 母乳一区在线观看| 久久国产福利| 校园激情久久| 亚洲一区二区三区777| 亚洲精品美女在线观看| 亚洲国产精品久久91精品| 久久久久一区二区三区四区| 欧美亚洲日本网站| 亚洲一本大道在线| 中国亚洲黄色| 亚洲视频福利| 中文国产成人精品久久一| 亚洲美女av在线播放| 在线激情影院一区| 一区一区视频| 亚洲国产91精品在线观看| 在线精品视频在线观看高清| 国内精品久久久| 国内久久精品| 亚洲第一综合天堂另类专| 国语自产在线不卡| 亚洲电影免费| 在线看国产日韩| 91久久亚洲| 亚洲精品一区二区三区樱花 | 欧美高清视频在线播放| 免费在线欧美黄色| 欧美精品1区| 欧美日韩亚洲另类| 欧美亚洲成人精品| 国产精品久久久久9999高清| 国产美女精品免费电影| 国产日韩亚洲欧美| 精品99一区二区| 亚洲电影在线观看| 日韩亚洲精品视频| 亚洲免费在线视频| 久久久www免费人成黑人精品| 免费日韩成人| 亚洲人午夜精品免费| 亚洲精品老司机| 中国女人久久久| 亚洲免费在线观看| 午夜精品一区二区三区在线播放| 在线视频精品一| 先锋影音久久久| 久久精品一区二区三区四区| 翔田千里一区二区| 免费不卡视频| 欧美三级网页| 国产欧美韩国高清| 9色精品在线| 久久久久国产一区二区|