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

QuXiao

每天進(jìn)步一點(diǎn)點(diǎn)!

  C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
  50 隨筆 :: 0 文章 :: 27 評(píng)論 :: 0 Trackbacks

解題報(bào)告


題目來源:

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

 

 

中文描述:

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

題目分析與算法模型

                通過分析可以知道,木條的具體長度是多少并不重要,只要滿足木條之間的長度不相等就可以了。為了簡單起見,就設(shè)n根木條的長度從小到大依次為1n。先從n比較小的情況找找規(guī)律,比如n=4,那么排列順序?yàn)椋?/span>1 3 2 41 4 2 32 1 4 32 3 1 42 4 1 33 1 4 23 2 4 13 4 1 24 1 3 24 2 3 1。可以看出,當(dāng)?shù)谝晃粩?shù)字X已經(jīng)確定時(shí),X后面的數(shù)字一定是先從1X-1開始考慮(比X低的那些木條),然后再從X+1n考慮(比X高的那些木條)。其實(shí),我們可以把排列方式分為兩種,一種是第一個(gè)木條比第二個(gè)木條高的排列方式,我們簡記為W方式;另一種是第一個(gè)木條比第二個(gè)木條低的排列方式,我們簡記為M方式。當(dāng)?shù)谝粋€(gè)數(shù)字確定時(shí),我們總是首先考慮所有W方式,等W方式全部考慮完,再去考慮所有的M方式。那么,n個(gè)木條的排列順序就為:

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

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

W2        

M2

Wn

Mn

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

           我們可以設(shè)M[x][n]為由n個(gè)長度不同的木條組成的柵欄中,以其中第x根木條開始的“M”型排列的個(gè)數(shù),W[x][n]為由n個(gè)長度不同的木條組成的柵欄中,以其中第x根木條開始的“W”型排列的個(gè)數(shù)。可以推出以下公式:

                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根是對應(yīng)的)

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

               

代碼:


#include <iostream>
using namespace std;
 
const int MAX = 21;
 
__int64 W[MAX][MAX];
__int64 M[MAX][MAX];
int used[MAX];         //用于輸出時(shí)判斷木條的使用情況
__int64 c;
int n;
 
void Input ()
{
        scanf("%d%I64d", &n, &c);
}
 
//利用遞歸進(jìn)行初始化
__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范圍內(nèi)第x根木條實(shí)際的位置
void Output (int x, int k)
{       
        int i;
        do
        {
               for (i=1; i<=k; i++)
               {
                        //1k中有以前已確定的木條,并且x木條比已確定的木
                        //條長度要長,那么x木條的長度應(yīng)加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;
}

 

心得:

                當(dāng)預(yù)感到一道題目可以用DP做時(shí),不妨先想出一個(gè)可以表示問題的狀態(tài),再看是否可以根據(jù)題中的所描述的操作進(jìn)行狀態(tài)轉(zhuǎn)移,并且看轉(zhuǎn)移之后的狀態(tài)是否是假設(shè)出的狀態(tài),即看自己假設(shè)出的表示問題的狀態(tài)是否具有最優(yōu)子結(jié)構(gòu)。若沒有,則應(yīng)該假設(shè)另一種狀態(tài)或者在原先狀態(tài)的基礎(chǔ)上進(jìn)行修改和加強(qiáng),直到找出符合最優(yōu)子結(jié)構(gòu)的狀態(tài)。

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

評(píng)論

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

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

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

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲一区日韩在线| 久久精品人人做人人爽| 99国产精品久久久久久久成人热| 在线免费高清一区二区三区| 激情综合色丁香一区二区| 国产一区二区看久久| 国产精品国产一区二区| 国产精品对白刺激久久久| 国产精品久久久久久久久久久久久 | 欧美国产日韩一区二区| 欧美激情一区二区三区在线| 亚洲国产精品传媒在线观看| 欧美成人精精品一区二区频| 91久久午夜| 亚洲免费福利视频| 亚洲性av在线| 美日韩精品免费| 欧美日韩在线一区| 国产欧美一区二区三区在线老狼 | 国产一区二区观看| 韩日在线一区| 一区二区激情视频| 欧美一区二区免费| 亚洲三级国产| 国产欧美日韩视频| 日韩亚洲欧美一区二区三区| 亚洲网站在线播放| 黄色精品一二区| 日韩亚洲不卡在线| 亚洲欧洲日本一区二区三区| 亚洲精品一区二区三区四区高清| 久久中文欧美| 欧美国产在线观看| 亚洲欧美成人一区二区三区| 性xx色xx综合久久久xx| 欲香欲色天天天综合和网| 99re亚洲国产精品| 伊人狠狠色j香婷婷综合| 日韩视频国产视频| 依依成人综合视频| 欧美专区18| 免费亚洲电影在线观看| 欧美怡红院视频| 亚洲性视频网址| 欧美 日韩 国产一区二区在线视频| 欧美激情精品久久久久久久变态| 国产精品播放| 亚洲乱码久久| 久久夜色精品| 99精品热视频| 一区二区三区免费在线观看| 久久久久国产精品www | 乱码第一页成人| 国产精品久久久久三级| 在线视频你懂得一区二区三区| 男女av一区三区二区色多| 欧美亚洲在线观看| 国产色综合网| 久久一区二区三区国产精品| 亚洲欧美在线免费| 国产日韩精品综合网站| 亚洲小说欧美另类社区| 欧美一区二区三区免费看| 欧美在线观看视频在线| 免费成人黄色av| 国产欧美日韩精品专区| 午夜一级在线看亚洲| 亚洲一区二区精品视频| 欧美日韩一区二区精品| 一区二区三区精密机械公司| 亚洲精品影院| 欧美日韩色综合| 亚洲欧美精品| 香蕉成人久久| 亚洲动漫精品| 亚洲精品影视在线观看| 国产精品久久久久免费a∨| 欧美一区二区三区视频在线 | 国产欧美日韩激情| 久久精品国产欧美亚洲人人爽| 久久国产乱子精品免费女| 亚洲国产精品ⅴa在线观看| 亚洲国产乱码最新视频| 欧美日韩国产综合视频在线观看中文 | 99精品久久久| 国产精品伦一区| 六月婷婷久久| 欧美日韩一级片在线观看| 欧美在线一级视频| 免费成人黄色片| 午夜精品99久久免费| 久久久久综合| 亚洲欧美成人网| 美女精品在线观看| 香蕉精品999视频一区二区 | 久久蜜桃精品| 亚洲视频久久| 久久午夜av| 欧美一区国产二区| 欧美96在线丨欧| 久久久91精品国产一区二区三区| 欧美高清在线观看| 久久午夜电影网| 欧美午夜精品久久久久久久| 免费观看一级特黄欧美大片| 国产精品高清一区二区三区| 农夫在线精品视频免费观看| 欧美三区不卡| 欧美激情中文不卡| 狠狠干狠狠久久| 亚洲天堂av图片| 99国产精品久久久久老师| 午夜日韩电影| 亚洲一区二区三区四区五区黄| 在线日本成人| 亚洲欧美在线免费| 亚洲无线视频| 午夜电影亚洲| 国产精品高清在线观看| 欧美成人午夜| 国产日韩免费| 亚洲一区成人| 亚洲香蕉伊综合在人在线视看| 卡通动漫国产精品| 久久夜色精品国产噜噜av| 国产精品一区二区你懂得| 日韩视频免费观看| 99国产欧美久久久精品| 欧美福利电影网| 欧美激情欧美激情在线五月| 在线观看日韩国产| 美国三级日本三级久久99| 免费成人黄色| 狠狠做深爱婷婷久久综合一区 | 欧美一区二区高清| 欧美日本在线| 日韩视频不卡中文| 一本色道久久99精品综合| 欧美激情国产日韩| 亚洲精品视频在线播放| 99天天综合性| 欧美午夜视频网站| 亚洲一区二区三区四区视频| 久久久久久久久岛国免费| 黄色成人av| 欧美劲爆第一页| 一个人看的www久久| 欧美一区二区日韩| 国产一区日韩二区欧美三区| 久久久国产成人精品| 免费视频久久| 亚洲肉体裸体xxxx137| 欧美日韩国产va另类| 亚洲欧美成人一区二区在线电影| 久久国产精品亚洲77777| 在线精品国产欧美| 欧美精品久久久久久久免费观看| 夜夜嗨av一区二区三区中文字幕| 欧美淫片网站| 亚洲日本va在线观看| 国产精品xxxxx| 欧美在线黄色| 亚洲精品日韩在线| 久久av一区二区| 91久久精品一区二区别| 欧美午夜宅男影院在线观看| 久久九九久久九九| 日韩一二三在线视频播| 久久精品视频免费| 99国产精品99久久久久久粉嫩| 国产精品理论片在线观看| 看片网站欧美日韩| 亚洲一区二区三区中文字幕| 欧美 日韩 国产 一区| 亚洲欧美大片| 亚洲片区在线| 国产区二精品视| 欧美日韩国产页| 久久久久久久综合| 亚洲一区二区三区成人在线视频精品| 噜噜噜久久亚洲精品国产品小说| 中文亚洲字幕| 亚洲黄色在线观看| 国产一区三区三区| 国产精品入口尤物| 91久久精品网| 久久精品毛片| 亚洲一区欧美一区| 日韩一级在线| 亚洲国产精品一区制服丝袜| 国产欧美一区二区色老头| 欧美日本不卡| 免费高清在线一区| 久久久99久久精品女同性| 亚洲欧美久久久久一区二区三区| 亚洲人在线视频| 亚洲欧洲精品天堂一级| 欧美国产一区在线| 榴莲视频成人在线观看| 久久久精品久久久久|