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

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

這樣很直觀的得到了一個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+分段統計  回復  更多評論   

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

# re: PKU1037 A decorative fence DP+分段統計  回復  更多評論   

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

# re: PKU1037 A decorative fence DP+分段統計  回復  更多評論   

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

# re: PKU1037 A decorative fence DP+分段統計  回復  更多評論   

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

# re: PKU1037 A decorative fence DP+分段統計  回復  更多評論   

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>
            免费成人高清视频| 久久婷婷亚洲| 一区二区三区**美女毛片| 欧美精品一区二区三区蜜臀| 99re热精品| 这里只有精品视频| 国产综合色一区二区三区| 久热精品在线| 欧美破处大片在线视频| 销魂美女一区二区三区视频在线| 亚洲香蕉成视频在线观看| 国产一区二区三区直播精品电影| 久久影视三级福利片| 欧美精品一区二区三区高清aⅴ| 亚洲欧美国内爽妇网| 久久精品人人做人人爽| 夜夜夜久久久| 久久国产一区二区| 99视频精品| 久久国产精品亚洲va麻豆| 99精品热视频| 久久久久国产精品一区二区| 一本色道久久综合亚洲精品高清| 一区二区三区日韩精品视频| 永久久久久久| 亚洲一区国产精品| 亚洲精品久久| 欧美一区二区三区婷婷月色| 亚洲美女色禁图| 午夜在线电影亚洲一区| 夜夜嗨av一区二区三区| 久久精品一区四区| 亚洲永久精品大片| 免费在线播放第一区高清av| 欧美淫片网站| 欧美日韩一区二区在线| 麻豆精品网站| 国产欧美一区二区在线观看| 亚洲日本一区二区三区| 激情懂色av一区av二区av| 中文国产成人精品久久一| 亚洲区欧美区| 久久天堂精品| 久久亚洲视频| 国产午夜精品美女毛片视频| 99精品视频免费观看视频| 亚洲国产日韩欧美在线图片| 欧美一区午夜视频在线观看| 亚洲在线免费视频| 欧美日韩亚洲三区| 亚洲三级色网| 亚洲精品美女久久7777777| 久久久97精品| 久久综合色播五月| 国产亚洲美州欧州综合国| 亚洲午夜久久久久久久久电影院| 99国产精品国产精品毛片| 欧美华人在线视频| 亚洲国产精品女人久久久| 亚洲国产精品一区二区www| 欧美一区二区视频在线| 久久激情中文| 国产主播喷水一区二区| 欧美在线视频免费| 久久久亚洲影院你懂的| 娇妻被交换粗又大又硬视频欧美| 欧美一区在线视频| 毛片一区二区三区| 亚洲国产黄色片| 欧美电影免费网站| 亚洲毛片在线免费观看| 在线性视频日韩欧美| 欧美午夜精品久久久久免费视| 日韩午夜电影av| 午夜在线电影亚洲一区| 国产亚洲综合性久久久影院| 久久久www成人免费无遮挡大片| 老司机精品福利视频| 亚洲日本va午夜在线影院| 欧美日韩极品在线观看一区| 亚洲午夜激情在线| 久久日韩粉嫩一区二区三区| 亚洲国产另类久久精品| 欧美美女操人视频| 亚洲免费视频在线观看| 免费不卡在线观看| 一区二区三区国产精品| 国产欧美精品日韩精品| 老司机久久99久久精品播放免费| 亚洲精品资源| 久久成人精品无人区| 亚洲国产一区二区a毛片| 欧美日韩在线播| 欧美在线在线| 亚洲精品视频免费| 欧美在线资源| 亚洲精品韩国| 国产一区二区三区四区三区四| 裸体女人亚洲精品一区| 亚洲图片欧洲图片日韩av| 免费在线观看一区二区| 亚洲综合精品自拍| 亚洲国产精品综合| 麻豆精品在线视频| 最新69国产成人精品视频免费| 亚洲国产精品日韩| 国产精品高潮久久| 麻豆成人综合网| 亚洲欧美国产77777| 亚洲人成网站色ww在线| 国产精品毛片在线看| 亚洲精品中文字幕有码专区| 黄色成人在线| 亚洲欧美色一区| 亚洲韩国一区二区三区| 亚洲美女色禁图| 国产日韩一区| 久久嫩草精品久久久精品一| 免费高清在线一区| 亚洲一区日韩| 久久av二区| 久久精品噜噜噜成人av农村| 亚洲一区二区三区精品在线| 国产无遮挡一区二区三区毛片日本| 亚洲精品久久久久| 亚洲一品av免费观看| 精品福利免费观看| 亚洲精品中文在线| 国产精品美女久久| 久久精品日产第一区二区三区| 亚洲欧美日韩一区二区在线| 久久一区二区三区超碰国产精品| 亚洲欧美日韩国产成人| 久久伊人精品天天| 中文国产亚洲喷潮| 你懂的国产精品| 欧美在线观看视频一区二区三区| 夜夜嗨一区二区| 亚洲精品在线免费| 亚洲精选久久| 一本大道久久精品懂色aⅴ| 亚洲人成网在线播放| 亚洲国产精品成人一区二区| 亚洲国产成人tv| 亚洲黄色免费| 日韩视频在线观看| 夜夜狂射影院欧美极品| 亚洲视频免费| 亚洲欧美一区二区三区极速播放 | 国产精品高潮视频| 欧美日韩在线一区| 国产精品va| 国产毛片精品国产一区二区三区| 国产欧美日韩精品一区| 国产视频在线观看一区二区| 含羞草久久爱69一区| 亚洲第一精品在线| 亚洲精品欧美一区二区三区| 亚洲无亚洲人成网站77777| 亚洲欧美一区二区视频| 久久成人人人人精品欧| 久久综合网络一区二区| 亚洲电影免费在线| 亚洲精品视频在线观看免费| 亚洲永久字幕| 久久夜色精品国产亚洲aⅴ| 欧美精品免费在线| 国产精品视频免费| 一区二区三区自拍| 一本久道久久综合中文字幕| 亚洲综合日韩| 母乳一区在线观看| 9久re热视频在线精品| 性色一区二区三区| 欧美电影免费观看高清| 国产精品一区二区你懂得 | 亚洲国产一区二区精品专区| 一区二区免费在线播放| 久久se精品一区二区| 亚洲国产精品久久精品怡红院| 一区二区三区精品国产| 久久久久久9| 国产精品久久久久毛片大屁完整版| 国产伪娘ts一区| 一本一本久久a久久精品综合妖精| 久久成人免费日本黄色| 亚洲另类一区二区| 久久久久欧美精品| 欧美伦理91i| 国产精品试看| 国产毛片精品视频| 亚洲第一区在线观看| 亚洲一区二区三区国产| 国产精品美女久久久浪潮软件| 欧美高清视频在线播放| 欧美一区二区三区在线观看视频 | 亚洲欧美日韩国产| 久久在线91| 国产视频在线观看一区二区三区| 亚洲精品一区二区三区婷婷月 |