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

oyjpArt ACM/ICPC算法程序設(shè)計空間

// 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)計  回復(fù)  更多評論   

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

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

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

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

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

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

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

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

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>
            亚洲自拍偷拍色片视频| 伊人天天综合| 亚洲人成在线影院| 国产一区二区久久精品| 亚洲蜜桃精久久久久久久| 在线观看免费视频综合| 亚洲综合社区| 午夜精品一区二区三区在线视| 欧美成人免费全部| 美女精品国产| 好吊视频一区二区三区四区| 亚洲欧美激情一区二区| 亚洲欧美电影在线观看| 欧美丝袜一区二区三区| 亚洲日韩中文字幕在线播放| 亚洲日本中文字幕区| 久久这里只有精品视频首页| 久久婷婷麻豆| 禁断一区二区三区在线 | 久久久福利视频| 国产精品免费电影| 亚洲视屏一区| 欧美一区二区成人6969| 国产精品一区二区三区四区五区 | 蜜桃av综合| 欧美成人精品一区| 亚洲第一视频网站| 美女999久久久精品视频| 亚洲第一精品夜夜躁人人爽| 亚洲高清免费| 欧美激情久久久久| 亚洲精品欧美| 亚洲欧美日本日韩| 国产欧美一区二区三区在线老狼 | 99精品国产福利在线观看免费| aaa亚洲精品一二三区| 欧美日韩国产一级片| 宅男精品视频| 久久久久久久久久久一区| 精品1区2区| 欧美—级在线免费片| 在线综合亚洲| 久久久久一区二区| 亚洲国产精品视频一区| 欧美日韩国产高清| 亚洲欧美成人一区二区三区| 久久亚洲精品伦理| 99re热这里只有精品视频| 国产精品久在线观看| 久久激情五月婷婷| 亚洲人成77777在线观看网| 亚洲嫩草精品久久| 在线成人激情视频| 欧美日韩国产片| 欧美一区二区在线免费观看| 欧美韩日一区| 欧美一区二区三区精品电影| 亚洲国产精品一区二区第一页 | 久久本道综合色狠狠五月| 欧美福利视频一区| 亚洲欧美成人网| 亚洲第一精品久久忘忧草社区| 欧美日韩国产不卡| 欧美在线视频免费观看| 亚洲人被黑人高潮完整版| 久久狠狠亚洲综合| 99视频+国产日韩欧美| 国产有码一区二区| 欧美日韩福利在线观看| 久久久精品国产免大香伊| 日韩视频永久免费观看| 久久亚洲二区| 午夜精品美女自拍福到在线| 91久久极品少妇xxxxⅹ软件| 国产欧美日韩精品在线| 欧美日韩国内自拍| 久久夜色精品国产| 欧美一区日韩一区| 亚洲手机视频| 日韩视频在线你懂得| 欧美二区不卡| 久久中文字幕导航| 久久国产乱子精品免费女| 亚洲一区二区三区中文字幕| 亚洲日本免费电影| 亚洲第一在线综合在线| 国模吧视频一区| 国产欧美精品一区| 国产精品久久久久久久久果冻传媒 | 亚洲少妇在线| 亚洲精品一区二区三区不| 好看不卡的中文字幕| 国产久一道中文一区| 国产精品福利影院| 欧美人体xx| 欧美精品18| 欧美连裤袜在线视频| 欧美成年人网| 欧美国产第一页| 欧美大片一区| 欧美精品尤物在线| 欧美激情小视频| 欧美激情综合色| 欧美区在线观看| 欧美精品在线免费观看| 欧美区亚洲区| 欧美日韩一区二区三区视频| 欧美精品网站| 欧美日韩综合| 国产精品美女久久久久aⅴ国产馆| 欧美日韩在线视频一区| 欧美亚洲成人网| 国产精品久久久久久超碰 | 在线精品在线| 亚洲国产精品免费| 亚洲另类黄色| 亚洲午夜一级| 欧美在线亚洲一区| 久久亚洲捆绑美女| 欧美高清视频在线| 最新69国产成人精品视频免费| 亚洲精品国产视频| 亚洲香蕉网站| 久久xxxx| 欧美成年人视频网站| 欧美日韩一区在线播放| 国产精品私拍pans大尺度在线| 国产日韩欧美夫妻视频在线观看| 国产一区二区三区久久久| 亚洲夫妻自拍| 亚洲一区中文| 麻豆9191精品国产| 亚洲精品视频二区| 亚洲欧美三级伦理| 蜜桃av综合| 国产精品欧美一区喷水| 伊人一区二区三区久久精品| 一区二区三区 在线观看视| 欧美一区91| 亚洲国产精品一区| 亚洲午夜久久久久久久久电影网| 欧美中日韩免费视频| 欧美激情影院| 国产专区欧美精品| 一区二区三区国产| 久久免费国产精品1| 日韩午夜电影av| 久久久激情视频| 欧美三级不卡| 亚洲激情图片小说视频| 欧美伊人精品成人久久综合97| 欧美国产日韩一区二区三区| 亚洲主播在线播放| 欧美激情综合色| 精品91在线| 亚洲欧美综合网| 亚洲韩国日本中文字幕| 欧美一级网站| 国产精品久久二区| 亚洲精品永久免费精品| 久久综合999| 亚洲欧美日韩综合一区| 欧美区在线观看| 亚洲第一精品夜夜躁人人爽| 久久国产精品黑丝| 一区二区三区四区五区精品视频| 久热精品视频在线免费观看| 国产欧美亚洲视频| 亚洲一区二区三区四区中文| 亚洲国产精品va在线看黑人动漫| 欧美一区二区日韩| 国产精品免费区二区三区观看| 亚洲精选国产| 亚洲国产精品第一区二区三区| 久久久国产成人精品| 国产欧美日本一区视频| 亚洲欧美国产精品桃花| 亚洲精品一级| 欧美日本高清一区| 日韩一级不卡| 亚洲欧洲一级| 欧美精品首页| 日韩亚洲在线观看| 91久久一区二区| 欧美高清不卡| 亚洲伦理在线免费看| 亚洲国内精品| 欧美激情在线观看| 日韩一区二区免费高清| 91久久在线视频| 欧美日韩精品欧美日韩精品一| 亚洲精品国产系列| 亚洲激情视频网站| 欧美精品亚洲二区| 国产精品99久久久久久久久| 亚洲精品日韩在线| 欧美色道久久88综合亚洲精品| 在线性视频日韩欧美| 一区二区日韩| 国产精品自在在线|