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

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>
            欧美激情黄色片| 午夜精品影院在线观看| 欧美va亚洲va香蕉在线| 久久蜜桃av一区精品变态类天堂| 国产精品亚洲综合色区韩国| 久久av一区| 久久免费高清| 亚洲免费在线视频一区 二区| 国产精品爽爽ⅴa在线观看| 欧美一区观看| 麻豆精品传媒视频| 在线视频你懂得一区二区三区| 制服丝袜激情欧洲亚洲| 国产一区二区久久| 亚洲国产小视频| 国产精品丝袜xxxxxxx| 久久偷看各类wc女厕嘘嘘偷窃| 可以看av的网站久久看| 亚洲性夜色噜噜噜7777| 欧美一区二区三区在| 日韩视频永久免费| 亚洲欧美日韩精品久久| 亚洲精品乱码久久久久| 亚洲影院免费观看| 亚洲精品国精品久久99热| 亚洲欧美日韩爽爽影院| 日韩一级精品| 久久久久欧美精品| 午夜精品成人在线| 欧美不卡一区| 久久在线免费观看视频| 欧美亚男人的天堂| 亚洲国产精品嫩草影院| 国产亚洲美州欧州综合国| 亚洲欧洲精品一区二区三区| 国产日韩欧美精品综合| 9人人澡人人爽人人精品| 在线电影一区| 欧美中文在线观看| 欧美亚洲综合另类| 欧美视频一区二区三区| 欧美粗暴jizz性欧美20| 国产一区自拍视频| 亚洲影院高清在线| 亚洲自拍啪啪| 欧美四级伦理在线| 亚洲欧洲日本专区| 91久久极品少妇xxxxⅹ软件| 欧美中日韩免费视频| 欧美一区二视频| 国产精品毛片| 亚洲午夜女主播在线直播| 一区二区激情小说| 欧美日本网站| 99国产精品久久久| 一级成人国产| 欧美日韩亚洲一区| 国产一区二区三区在线观看精品| 亚洲色无码播放| 亚洲一区二区三区视频播放| 欧美日韩国产不卡| 亚洲品质自拍| 中文国产成人精品| 国产精品欧美激情| 午夜国产精品视频| 久久久久久综合| 在线成人性视频| 男女精品网站| 亚洲肉体裸体xxxx137| 日韩一区二区精品| 国产精品久久久久高潮| 亚洲欧美日韩另类精品一区二区三区| 亚洲欧美电影院| 国产亚洲精品久久久久婷婷瑜伽| 香蕉成人啪国产精品视频综合网| 久久精品国产一区二区三区| 国内精品伊人久久久久av影院 | 午夜亚洲影视| 国产一级一区二区| 老色批av在线精品| 日韩午夜一区| 久久国产欧美日韩精品| 好吊色欧美一区二区三区四区| 久久午夜精品一区二区| 亚洲黑丝在线| 午夜国产一区| 亚洲福利在线视频| 欧美日韩国产首页| 欧美一区二区三区四区在线观看地址| 欧美xart系列高清| 亚洲综合999| 亚洲第一偷拍| 国产精品久久久久久户外露出| 香蕉久久a毛片| 亚洲国产精品久久久久婷婷884| 久久久噜噜噜久噜久久| 亚洲黄页视频免费观看| 欧美在线视频在线播放完整版免费观看| 国产真实乱子伦精品视频| 欧美成人资源网| 午夜精品短视频| 亚洲三级影院| 噜噜噜91成人网| 亚洲欧美一区二区三区久久 | 国产精品豆花视频| 久久婷婷综合激情| 亚洲综合激情| 夜色激情一区二区| 欧美高清在线| 久久久亚洲国产天美传媒修理工| 亚洲精品偷拍| 一区二区亚洲| 国产伦精品一区二区三区免费| 美女精品网站| 久久久久久久精| 亚洲欧美日韩综合国产aⅴ| 亚洲精品久久久久久久久久久| 久久精品在线观看| 欧美亚洲一级片| 亚洲一级黄色| 一区二区三区波多野结衣在线观看| 狠狠色丁香婷婷综合| 国产精品一区二区三区观看| 欧美日韩国产成人在线91| 狼狼综合久久久久综合网| 亚洲欧美精品伊人久久| 伊人婷婷久久| 国产一区二区中文| 国产乱码精品一区二区三区忘忧草 | 久久久久久尹人网香蕉| 午夜在线观看免费一区| 亚洲午夜激情网页| 99在线精品视频| 一区二区三区 在线观看视频| 亚洲国产成人av在线| 亚洲高清网站| 亚洲韩国精品一区| 亚洲国产成人tv| 亚洲国产精品久久久久久女王| 男人插女人欧美| 欧美+日本+国产+在线a∨观看| 久久精品首页| 美日韩精品视频免费看| 欧美大香线蕉线伊人久久国产精品| 久久久久久久久久看片| 久久久综合网| 欧美成人午夜免费视在线看片| 久久综合五月天婷婷伊人| 久久裸体视频| 女同一区二区| 亚洲人成高清| 中文日韩欧美| 香蕉乱码成人久久天堂爱免费 | 久久精品男女| 久久久久久久激情视频| 久久影视三级福利片| 美日韩精品免费| 亚洲麻豆av| 性色av香蕉一区二区| 久久人人九九| 欧美日韩一区二区国产| 国产精品一二三| 激情国产一区二区| 日韩亚洲精品电影| 性亚洲最疯狂xxxx高清| 毛片基地黄久久久久久天堂| 亚洲欧洲一区二区三区久久| 一区二区三区视频观看| 欧美在线影院在线视频| 欧美成熟视频| 国产精品一区二区久久久久| 在线播放视频一区| 这里只有视频精品| 久久婷婷综合激情| 99视频热这里只有精品免费| 午夜精品久久久久影视| 欧美va亚洲va国产综合| 国产精品久久久久久久久| 伊人夜夜躁av伊人久久| 亚洲午夜视频| 欧美成人有码| 亚洲综合日韩中文字幕v在线| 免费看黄裸体一级大秀欧美| 国产精品久久久久9999高清| 在线观看日韩av先锋影音电影院| 99国产精品久久久久久久久久 | 日韩午夜电影在线观看| 久久9热精品视频| 欧美视频二区36p| 亚洲欧美综合国产精品一区| 欧美 日韩 国产精品免费观看| 国产日本欧美在线观看| 99人久久精品视频最新地址| 久久婷婷国产麻豆91天堂| 亚洲视频在线视频| 欧美精彩视频一区二区三区| 一区二区三区在线免费播放| 西瓜成人精品人成网站| 亚洲精品美女免费| 欧美高清在线视频|