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

oyjpArt ACM/ICPC算法程序設計空間

// I am new in programming, welcome to my blog
I am oyjpart(alpc12, 四城)
posts - 224, comments - 694, trackbacks - 0, articles - 6

A decorative fence
Time Limit:1000MS  Memory Limit:10000K
Total Submit:1548 Accepted:440

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


也算是DP+分段統(tǒng)計中的經(jīng)典題了 呵呵
DP的狀態(tài)表示如下
dp[style][n][i][j] :
style 代表走向 0 代表向上(也就是下次要向下) 1代表向下
n代表總共的fence數(shù)
i代表比當前選擇的fence高的fence數(shù)(注意 當前fence是一個隱藏的參數(shù) 因為該狀態(tài)不需要知道當前fence是哪個 只需要知道有多少比這個fence高 多少比這個fence低 就可以代表整個狀態(tài))
j代表比當前選擇的fence低的fence數(shù)

這樣很直觀的得到了一個DP方程

    dp[0][i][j][k] += dp[1][i-1][j-m][k+m-1]; (m = 1 ... j (inclusive))

    dp[1][i][j][k] += dp[0][i-1][j+m-1][k-m];   (m = 1... k(inclusive))

具體請參考源代碼

 1#include <stdio.h>
 2#include <string.h>
 3
 4const int N = 21;
 5__int64 dp[2][N][N][N];
 6int n;
 7__int64 idx;
 8bool chk[N];
 9
10void pre() {
11    int i, j, m;
12
13    memset(dp, 0, sizeof(dp));
14
15    dp[0][1][0][0= 1;
16    dp[1][1][0][0= 1;
17
18    for(i = 2; i <= 20++i) {
19        for(j = 0; j < i; ++j) {
20            int k = i - j - 1;
21            for(m = 1; m <= j; ++m) 
22                dp[0][i][j][k] += dp[1][i-1][j-m][k+m-1];
23            for(m = 1; m <= k; ++m)
24                dp[1][i][j][k] += dp[0][i-1][j+m-1][k-m];
25        }
26    }
27}
28
29void DFS(int nowint last, int style, __int64 idx) {
30    if(now <= 0) return;
31    int i, j;
32    for(i = 0; i < n; ++i) if(!chk[i]) {
33        if(style == 0 && i < last) continue;
34        if(style == 1 && i > last) return;
35
36        chk[i] = true;
37        int big = 0, small = 0;
38        for(j = 0; j < n; ++j) if(!chk[j]) {
39            if(j < i) small++;
40            if(j > i) big++;
41        }
42
43        if(style == 0 || style == -1) {
44            if(idx > dp[1][now][big][small]) idx -= dp[1][now][big][small];
45            else {
46                printf("%d ", i+1);
47                DFS(now-1, i, 1, idx);    return;
48            }
49        }
50
51        if(style == 1 || style == -1) {
52            if(idx > dp[0][now][big][small]) idx -= dp[0][now][big][small];
53            else {
54                printf("%d ", i+1);
55                DFS(now-1, i, 0, idx);    return;
56            }
57        }
58        chk[i] = false;
59    }
60}
61
62int main() {
63    int ntc;
64    pre();
65    scanf("%d "&ntc);
66    while(ntc--) {
67        scanf("%d %I64d"&n, &idx);
68        memset(chk, false, sizeof(chk));
69        DFS(n, -1-1, idx);
70        putchar('\n');
71    }
72    return 0;
73}

Feedback

# re: PKU1037 A decorative fence DP+分段統(tǒng)計  回復  更多評論   

2007-08-18 15:57 by deoxyz
你的最后一維根本不需要啊.......這樣空間復雜度會下降不少啊...

# re: PKU1037 A decorative fence DP+分段統(tǒng)計  回復  更多評論   

2007-08-18 16:27 by oyjpart
真的么?
那你怎么寫的呢?

# re: PKU1037 A decorative fence DP+分段統(tǒng)計  回復  更多評論   

2007-08-19 10:08 by deoxyz
就你的這個程序的話 直接把所有有關最后一維的東西全部刪掉就行了,第三維完全可以用前兩維表示~而且輸出可以不用遞歸會快點~ 我ACM也剛學1年多點 有空交流交流 我QQ120148455

# re: PKU1037 A decorative fence DP+分段統(tǒng)計  回復  更多評論   

2007-08-19 11:05 by oyjpart
恩 是的

# re: PKU1037 A decorative fence DP+分段統(tǒng)計  回復  更多評論   

2007-10-10 13:28 by floyd635
的確只要三維就可以完成...
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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国产精品澳门| 国内精品视频一区| 女生裸体视频一区二区三区| 久久字幕精品一区| 日韩亚洲国产精品| 在线视频亚洲一区| 国产人成一区二区三区影院| 久久久www成人免费精品| 久久免费视频在线| 99精品免费| 性欧美在线看片a免费观看| 激情婷婷亚洲| 亚洲三级视频| 国产精品久久久久久亚洲毛片| 欧美中文字幕不卡| 免播放器亚洲| 亚洲主播在线| 乱中年女人伦av一区二区| 中文日韩欧美| 久久精品盗摄| 亚洲午夜黄色| 看欧美日韩国产| 亚洲一区精品视频| 久久一区二区三区国产精品 | 一区二区高清视频在线观看| 亚洲一区久久| 亚洲国产色一区| 亚洲一卡久久| 亚洲精品老司机| 亚洲欧美自拍偷拍| 正在播放亚洲| 欧美sm极限捆绑bd| 久久精品视频免费播放| 欧美风情在线观看| 老妇喷水一区二区三区| 国产精品vip| 亚洲精品国久久99热| 国产自产女人91一区在线观看| 亚洲国产欧美在线人成| 国产一区二区三区高清在线观看| 亚洲国产精品综合| 狠狠色狠狠色综合日日91app| 夜夜嗨av一区二区三区中文字幕| 亚洲国产精品成人综合色在线婷婷| 亚洲欧美日韩人成在线播放| 一区二区三区四区五区精品视频| 久久久久久伊人| 久久成人免费电影| 国产精品二区三区四区| 亚洲精品少妇30p| 亚洲人体大胆视频| 免费亚洲一区二区| 欧美大片在线看| 亚洲电影免费观看高清完整版| 久久精品卡一| 久久嫩草精品久久久精品一| 国产模特精品视频久久久久| 中文网丁香综合网| 亚洲小视频在线观看| 欧美日韩视频免费播放| 亚洲精品乱码久久久久| 亚洲精品偷拍| 欧美精品一卡二卡| 99国产精品久久久久久久久久| 一区二区三区国产在线观看| 欧美日韩dvd在线观看| 最新国产の精品合集bt伙计| 日韩午夜在线| 欧美日韩视频第一区| 一区二区福利| 亚洲欧美一区二区视频| 国产精品一卡二卡| 欧美在线影院| 欧美成人免费一级人片100| 亚洲东热激情| 欧美精品一区二区高清在线观看| aa国产精品| 久久岛国电影| 在线观看亚洲精品| 欧美国产视频在线观看| 9色国产精品| 久久精品天堂| 亚洲精品久久久久久下一站| 欧美日韩国产不卡| 亚洲女性喷水在线观看一区| 久久影院午夜论| 亚洲精品日本| 国产热re99久久6国产精品| 久久免费国产精品1| 亚洲精品乱码久久久久久久久| 亚洲免费在线观看视频| 国产日韩欧美精品一区| 欧美福利视频在线| 亚洲综合日本| 亚洲电影欧美电影有声小说| 亚洲一区二区三区高清| 国产日韩专区| 欧美精品在线观看| 欧美一区激情| 99re6热在线精品视频播放速度| 午夜亚洲激情| 亚洲欧洲在线播放| 国产亚洲一本大道中文在线| 欧美精品一卡二卡| 久久精品国产清自在天天线| 亚洲毛片在线看| 蜜桃久久精品乱码一区二区| 亚洲欧美日本在线| 亚洲精品美女久久久久| 狠狠色狠狠色综合日日91app| 欧美视频中文字幕在线| 久久夜色精品国产亚洲aⅴ| 亚洲一区亚洲二区| 亚洲国产毛片完整版| 久久久久www| 亚洲欧美日韩精品| 夜夜嗨av一区二区三区四区| 亚洲国产日韩欧美一区二区三区| 国产精品久久久久久一区二区三区 | 宅男噜噜噜66一区二区66| 激情欧美亚洲| 国产欧美精品日韩| 欧美三区在线视频| 欧美成人一区二区| 久久久久网址| 久久精品女人的天堂av| 午夜精品国产更新| 亚洲一区在线观看视频| 日韩午夜在线视频| 亚洲人成在线播放网站岛国| 欧美激情网站在线观看| 久久午夜精品| 久久久久免费视频| 久久精品人人做人人爽电影蜜月| 亚洲一级片在线看| 亚洲一区二区精品在线观看| 亚洲午夜精品久久| 亚洲深夜av| 亚洲欧美中日韩| 午夜欧美不卡精品aaaaa| 亚洲影院高清在线| 亚洲宅男天堂在线观看无病毒| 一区二区三区日韩| 一区二区三区高清不卡| 亚洲校园激情| 性色av一区二区三区红粉影视| 午夜欧美大片免费观看| 欧美中文在线视频| 久久夜色精品国产欧美乱| 久久久天天操| 欧美国产成人精品| 亚洲国产清纯| 一本一本久久a久久精品综合麻豆 一本一本久久a久久精品牛牛影视 | 久久精品在线| 欧美成年人视频网站| 欧美激情一区二区| 国产精品草莓在线免费观看| 国产精品视频一二| 国产一区欧美| 亚洲国产精品久久久久久女王 | 国产精品sss| 国产欧美精品一区| 在线欧美小视频| 在线视频免费在线观看一区二区| 亚洲午夜久久久久久久久电影网| 欧美一区二区久久久| 欧美3dxxxxhd| aa亚洲婷婷| 久久精品理论片| 欧美激情精品久久久久久| 国产精品免费aⅴ片在线观看| 激情综合五月天| 亚洲少妇在线| 免费黄网站欧美| 一本色道久久综合狠狠躁篇怎么玩| 亚洲欧美影院| 欧美大胆a视频| 国产精品推荐精品| 亚洲人成人99网站| 欧美一区网站| 亚洲精品久久久一区二区三区| 亚洲一区亚洲| 欧美人体xx| 韩国av一区二区三区四区| 在线视频亚洲| 欧美成人精品一区| 性欧美精品高清| 欧美日韩免费看| 最新日韩在线| 久久久久久9999| 亚洲一区二区三区在线看| 欧美成人a∨高清免费观看| 国产精品欧美日韩一区| 亚洲九九精品| 欧美aⅴ一区二区三区视频|