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

QuXiao

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

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

                PKU 2201 Cartesian Tree

分類:

                RMQ

原文:

 

Cartesian Tree

Time Limit: 10000MS


Memory Limit: 65536K

Total Submissions: 1196


Accepted: 423

Case Time Limit: 2000MS

Description

Let us consider a special type of a binary search tree, called a cartesian tree. Recall that a binary search tree is a rooted ordered binary tree, such that for its every node x the following condition is satisfied: each node in its left subtree has the key less then the key of x, and each node in its right subtree has the key greater then the key of x.
That is, if we denote left subtree of the node x by L(x), its right subtree by R(x) and its key by kx then for each node x we have

  • if y L(x) then ky < kx
  • if z R(x) then kz > kx


The binary search tree is called cartesian if its every node x in addition to the main key kx also has an auxiliary key that we will denote by ax, and for these keys the heap condition is satisfied, that is

  • if y is the parent of x then ay < ax


Thus a cartesian tree is a binary rooted ordered tree, such that each of its nodes has a pair of two keys (k, a) and three conditions described are satisfied.
Given a set of pairs, construct a cartesian tree out of them, or detect that it is not possible.

Input

The first line of the input file contains an integer number N -- the number of pairs you should build cartesian tree out of (1 <= N <= 50 000). The following N lines contain two numbers each -- given pairs (ki, ai). For each pair |ki|, |ai| <= 30 000. All main keys and all auxiliary keys are different, i.e. ki != kj and ai != aj for each i != j.

Output

On the first line of the output file print YES if it is possible to build a cartesian tree out of given pairs or NO if it is not. If the answer is positive, on the following N lines output the tree. Let nodes be numbered from 1 to N corresponding to pairs they contain as they are given in the input file. For each node output three numbers -- its parent, its left child and its right child. If the node has no parent or no corresponding child, output 0 instead.
The input ensure these is only one possible tree.

Sample Input

7

5 4

2 2

3 9

0 5

1 3

6 6

4 11

Sample Output

YES

2 3 6

0 5 1

1 0 7

5 0 0

2 4 0

1 0 0

3 0 0

Source

Northeastern Europe 2002, Northern Subregion

 

 

 

 

中文描述:

                有一種二叉樹(shù),叫笛卡爾樹(shù),樹(shù)的節(jié)點(diǎn)有兩個(gè)值:kak值滿足二叉排序樹(shù)的性質(zhì),a值滿足最小堆的性質(zhì)。即如果某個(gè)根節(jié)點(diǎn)root有兩個(gè)子節(jié)點(diǎn)leftright,那么left.k < root.k < right.k,且root.a < left.aroot.a < right.a。給你N(1 <= N <= 50 000)個(gè)節(jié)點(diǎn),問(wèn)你是否可以構(gòu)造出一棵笛卡爾樹(shù)。

 

題目分析與算法模型

                一開(kāi)始,自己是想根據(jù)最小堆的性質(zhì),擁有最小a值的那個(gè)節(jié)點(diǎn)一定是樹(shù)的根,接著再找兩個(gè)次小a值的節(jié)點(diǎn),它們必然是根的兩個(gè)子節(jié)點(diǎn),再根據(jù)k值決定節(jié)點(diǎn)是左兒子還是右兒子,然后再以此類推…………,但是在下一層就不對(duì)了。因?yàn)椴⒉皇菢?shù)的下一層節(jié)點(diǎn)的a值一定比上一層節(jié)點(diǎn)的a值大(它們不一定在同一棵子樹(shù))。

                可以換一個(gè)思維,把注意力放在k值上。要知道,如果對(duì)一顆二叉排序樹(shù)進(jìn)行前序搜索,k值是從小到大排序的。如果某個(gè)節(jié)點(diǎn)是根,那么它左邊的節(jié)點(diǎn)就構(gòu)成左子樹(shù),它右邊的節(jié)點(diǎn)就構(gòu)成右子樹(shù)?,F(xiàn)在,那個(gè)根節(jié)點(diǎn)是哪一個(gè)?就是那個(gè)a值最小的節(jié)點(diǎn)!所以,我們可以對(duì)k值進(jìn)行排序,現(xiàn)在整個(gè)區(qū)間內(nèi)找到a值最小的節(jié)點(diǎn),他就是根。接著再在左邊和右邊的區(qū)間內(nèi)各找一個(gè)a值最小的節(jié)點(diǎn),看它們的節(jié)點(diǎn)的k值與根節(jié)點(diǎn)的k值是否滿足二叉排序樹(shù)的性質(zhì),如果滿足,就用相同的方法在左、右區(qū)間遞歸建立子樹(shù);如果不滿足,表示無(wú)法構(gòu)成笛卡爾樹(shù)。


                接下來(lái)的問(wèn)題就是,如何在一區(qū)間里找到最小的a值?最容易想到的就是O(n)復(fù)雜度的線性查找,但在此題中,N最大為50000,并且當(dāng)在一個(gè)較大區(qū)間內(nèi)查找到一個(gè)最值后,又要在一個(gè)較小的區(qū)間內(nèi)查找另一個(gè)最值,一些節(jié)點(diǎn)被查找了多次,造成時(shí)間的浪費(fèi)。那么,怎么高效的進(jìn)行多次的區(qū)間查詢呢?RMQ是一個(gè)不錯(cuò)的解決方法。大致思想是:先對(duì)區(qū)間內(nèi)的數(shù)進(jìn)行預(yù)處理,計(jì)算出從某一下標(biāo)開(kāi)始的某一特定長(zhǎng)度的最值。當(dāng)查找某一區(qū)間的最值時(shí),就可以把這個(gè)區(qū)間分解成一個(gè)或兩個(gè)已預(yù)先算出最值得區(qū)間,這樣就可以用O(1)的復(fù)雜度算出最值了。(具體講解請(qǐng)查閱相關(guān)資料)

 

代碼:

#include <iostream>

#include <cmath>

#include <algorithm>

using namespace std;

 

const int MAX = 50005;

 

struct Node

{

          int index;

          int k, a;

          int parent, left, right;

};

 

Node node[MAX];

int left, right;

int f[MAX][16];                  //f[i][j] is the index of the min a from i

                                 //to i + 2^j - 1

int n;

 

bool cmpByK (Node n1, Node n2)

{

          return ( n1.k < n2.k );

}

 

bool cmpByIndex (Node n1, Node n2)

{

          return ( n1.index < n2.index );

}

 

void Input ()

{

          int i;

          scanf("%d", &n);

          for (i=0; i<n; i++)

          {

                  scanf("%d%d", &node[i].k, &node[i].a);

                  node[i].index = i + 1;

          }

}

 

int Max (int a, int b)

{

          return ( a>b?a:b );

}

 

 

int Min (int a, int b)

{

          return ( a<b?a:b );

}

 

 

void Initial ()

{

          int i, k, m;

          sort(node, node+n, cmpByK);

 

 

          //RMQ

          for (i=0; i<n; i++)

                  f[i][0] = i;

 

          m = floor(log(double(n)) / log(double(2))) + 1;

          for (k=1; k<m; k++)

          {

                  for (i=0; i<n; i++)

                  {

                         f[i][k] = f[i][k-1];

                         if ( i + (1<<(k-1)) < n )

                         {

                                 if ( node[f[i][k-1]].a > node[f[i + (1<<(k-1))][k-1]].a )

                                         f[i][k] = f[i + (1<<(k-1))][k-1];

                         }

                  }

          }

}

 

 

int MinAIndex (int i, int j)

{

          int k;

          k = floor( log(double(j-i+1)) / log(double(2)) );

          if (node[f[i][k]].a <= node[f[j - (1<<k) + 1][k]].a)

                  return f[i][k];

          else

                  return f[j - (1<<k) + 1][k];

}

 

bool MakeTree (int i, int j)

{

          if ( i == j )

          {

                  node[i].left = node[i].right = 0;

                  return true;

          }

          int rootIndex, leftIndex, rightIndex;

          bool check1, check2;

          rootIndex = MinAIndex(i, j);

         

          if ( rootIndex != i )

                  leftIndex = MinAIndex(i, rootIndex-1);

          if ( rootIndex != j )

                  rightIndex = MinAIndex(rootIndex+1, j);

 

          check1 = true;

          if ( rootIndex != i && node[rootIndex].k > node[leftIndex].k )

          {

                  node[rootIndex].left = node[leftIndex].index;

                  node[leftIndex].parent = node[rootIndex].index;

                  check1 = MakeTree(i, rootIndex-1);

          }

          check2 = true;

          if ( rootIndex != j && node[rootIndex].k < node[rightIndex].k )

          {

                  node[rootIndex].right = node[rightIndex].index;

                  node[rightIndex].parent = node[rootIndex].index;

                  check2 = MakeTree(rootIndex+1, j);

          }

 

          return ( check1 && check2 );

}

         

void Solve ()

{

          if ( MakeTree(0, n-1) )

          {

                  printf("YES\n");

                  sort(node, node+n, cmpByIndex);

                  for (int i=0; i<n; i++)

                  {

                         printf("%d %d %d\n", node[i].parent, node[i].left, node[i].right);

                  }

          }

          else

          {

                  printf("NO\n");

          }

}

 

int main ()

{

          Input ();

          Initial ();

          Solve ();

 

          return 0;

}

 

posted on 2008-04-25 21:27 quxiao 閱讀(1018) 評(píng)論(1)  編輯 收藏 引用 所屬分類: ACM

評(píng)論

# re: PKU 2201 Cartesian Tree[未登錄](méi) 2009-05-12 12:20 k
笛卡爾樹(shù)在排好序的情況下有o(n)構(gòu)造法  回復(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>
            亚洲国产精品va| 亚洲激情欧美激情| 麻豆视频一区二区| 六月丁香综合| 欧美高清免费| 欧美精品二区三区四区免费看视频| 久久亚洲春色中文字幕久久久| 蜜桃av综合| 欧美日韩亚洲精品内裤| 国产精品欧美久久| 国产亚洲人成a一在线v站| 伊人久久综合97精品| 日韩一区二区精品视频| 欧美诱惑福利视频| 欧美激情一区在线| 亚洲无限乱码一二三四麻| 久久av一区二区三区亚洲| 欧美大尺度在线观看| 国产精品超碰97尤物18| 黄色国产精品一区二区三区| 亚洲免费黄色| 亚洲一级高清| 一区二区日韩欧美| 香蕉久久夜色精品| 老司机免费视频一区二区| 欧美日韩视频一区二区三区| 国产精品日韩高清| 亚洲福利在线看| 亚洲午夜激情在线| 久久精品国产精品亚洲综合| 麻豆精品视频在线| 在线视频日韩精品| 老司机aⅴ在线精品导航| 国产精品久久久久三级| 在线免费一区三区| 亚洲欧美一区二区原创| 欧美电影在线观看| 一区二区欧美在线观看| 牛牛国产精品| 国产日韩欧美黄色| 亚洲视频在线一区观看| 免费在线观看成人av| 午夜激情综合网| 欧美日韩一卡| 91久久精品美女高潮| 久久精品天堂| 亚洲女性裸体视频| 国产精品草莓在线免费观看| 亚洲日韩中文字幕在线播放| 久久久99久久精品女同性| 日韩午夜免费视频| 欧美激情精品久久久久久免费印度 | 久久久久免费观看| 中国亚洲黄色| 欧美日韩亚洲综合一区| 日韩午夜高潮| 亚洲国产毛片完整版| 欧美在线网站| 国产亚洲人成网站在线观看 | 亚洲福利国产| 久久亚洲私人国产精品va| 国产综合久久久久久鬼色| 欧美综合77777色婷婷| 亚洲性色视频| 国产区欧美区日韩区| 亚洲综合丁香| 亚洲一级影院| 国产伦精品一区二区三区免费迷| 在线亚洲精品| 亚洲一区在线播放| 国产精品永久| 久久电影一区| 中日韩视频在线观看| 性欧美xxxx大乳国产app| 欧美人与性动交α欧美精品济南到| 亚洲成色777777在线观看影院| 久久精品日产第一区二区| 午夜精品福利电影| 好吊妞这里只有精品| 亚洲色图自拍| 欧美激情欧美狂野欧美精品 | 欧美日韩一区在线观看视频| 一区二区毛片| 亚洲一区二区三区久久| 国产婷婷色一区二区三区四区| 久久久99久久精品女同性| 久久久五月天| 亚洲图片欧洲图片av| 新狼窝色av性久久久久久| 一区视频在线看| 91久久综合| 国产精品欧美精品| 免费亚洲视频| 欧美视频一区二区| 久久精品夜色噜噜亚洲aⅴ| 久久久噜噜噜久噜久久| 99re6这里只有精品| 亚洲在线一区二区三区| 永久免费精品影视网站| 99国产精品99久久久久久粉嫩| 国产农村妇女毛片精品久久麻豆| 免费在线欧美视频| 欧美性做爰毛片| 免费观看成人| 国产精品一区在线播放| 欧美国产精品人人做人人爱| 国产精品成人一区二区三区夜夜夜| 久久精品综合| 国产精品国产三级国产专播精品人 | 国产一区二区三区的电影 | 在线综合视频| 亚洲激情在线激情| 亚洲在线观看视频| 亚洲美女精品久久| 久久国产精品久久久久久久久久 | 欧美一级二级三级蜜桃| 欧美高清视频免费观看| 久久精品亚洲国产奇米99| 欧美激情欧美激情在线五月| 久久精品国产精品亚洲精品| 欧美高清一区二区| 亚洲国产欧美日韩另类综合| 欧美日本免费| 欧美日韩精品一区二区在线播放| 午夜在线电影亚洲一区| 欧美亚洲视频一区二区| 欧美粗暴jizz性欧美20| 国产精品视频xxx| 99国产欧美久久久精品| 亚洲福利视频三区| 久久狠狠亚洲综合| 欧美一区二区视频在线观看| 欧美少妇一区| 最新国产の精品合集bt伙计| 精品va天堂亚洲国产| 亚洲欧美国产日韩天堂区| 中文欧美字幕免费| 欧美日韩国产综合新一区| 亚洲国产高清一区| 亚洲国产精品小视频| 麻豆精品国产91久久久久久| 美女国产精品| 亚洲肉体裸体xxxx137| 欧美成人综合在线| 亚洲国产美女久久久久| 日韩网站免费观看| 欧美日韩色婷婷| 国产精品99久久久久久久女警 | 久久精品免视看| 久久综合色8888| 怡红院精品视频在线观看极品| 久久国产免费| 欧美成人亚洲成人| 亚洲免费电影在线| 国产精品爱啪在线线免费观看| 亚洲精品乱码视频 | 午夜精品久久久久久99热软件| 榴莲视频成人在线观看| 欧美在线视频在线播放完整版免费观看| 欧美另类一区二区三区| 日韩视频精品在线观看| 亚洲风情亚aⅴ在线发布| 欧美成人免费小视频| 亚洲性视频h| 午夜精彩视频在线观看不卡| 国产精品综合| 久久精品人人爽| 久久久久欧美精品| 亚洲国产另类精品专区| 亚洲日本中文| 亚洲日本中文字幕| 欧美日韩人人澡狠狠躁视频| 欧美调教vk| 国产精品久久久久免费a∨大胸| 国产精品美女xx| 久久这里只有| 久久婷婷国产综合尤物精品| 亚洲第一区在线观看| 欧美日本簧片| 久久成人免费视频| 亚洲精品欧美极品| 鲁鲁狠狠狠7777一区二区| 夜夜嗨网站十八久久| 国产一区二区精品久久| 欧美国产日产韩国视频| 性18欧美另类| 一区二区三区av| 欧美二区在线播放| 欧美一区二区三区精品电影| 亚洲精品黄色| 国产一级久久| 国产精品久久久久久福利一牛影视| 免费成人美女女| 欧美亚洲一区| 亚洲伊人伊色伊影伊综合网 | 蜜桃av一区二区三区| 亚洲欧美另类综合偷拍| 亚洲免费电影在线观看| 亚洲成色777777女色窝| 韩国av一区二区|