zoukankan      html  css  js  c++  java
  • poj 2201 Cartesian Tree 夜

    http://poj.org/problem?id=2201

    基础笛卡尔树

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    
    using namespace std;
    
    const int N=50005;
    struct node
    {
        int key;
        int value;
        int l,r,f;
        int I;
    }mem[N];
    int bhead,ri;
    bool cmp(node a,node b)
    {
        return a.key<b.key;
    }
    int f[N];
    int l[N];
    int r[N];
    void in(int w)
    {
        int up=ri;
        //cout<<up<<" "<<w<<":  "<<endl;
        while(up!=0&&mem[up].value>mem[w].value)
        {
            up=mem[up].f;
           // cout<<up<<" "<<mem[up].value<<endl;
        }//cout<<endl;
        if(up==0)
        {//cout<<"iiii"<<endl;
            mem[w].l=bhead;
            mem[w].f=0;
            mem[bhead].f=w;
            bhead=w;
        }else
        {//cout<<"IIIII"<<endl;
            mem[w].l=mem[up].r;
            mem[mem[up].r].f=w;
            mem[w].f=up;
            mem[up].r=w;
        }
        ri=w;
    }
    void dfs(int x)
    {
        if(x)
        {
            dfs(mem[x].l);
            dfs(mem[x].r);
            int i=mem[x].I;
            f[i]=mem[mem[x].f].I;
            l[i]=mem[mem[x].l].I;
            r[i]=mem[mem[x].r].I;
        }
    }
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            for(int i=1;i<=n;++i)
            {
                mem[i].I=i;
                scanf("%d %d",&mem[i].key,&mem[i].value);
            }
            sort(mem+1,mem+n+1,cmp);
            bhead=1;
            ri=1;
            mem[1].l=0;
            mem[1].r=0;
            mem[1].f=0;
            for(int i=2;i<=n;++i)
            {
                mem[i].l=0;
                mem[i].r=0;
                in(i);//cout<<mem[bhead].I<<endl;
            }
            mem[0].I=0;
            dfs(bhead);
            printf("YES\n");
            for(int i=1;i<=n;++i)
            {
                printf("%d %d %d\n",f[i],l[i],r[i]);
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    详解java定时任务
    Java之Exception
    Java设计模式
    Java中的static关键字解析
    浅析Java中的final关键字
    深入理解Java的接口和抽象类
    一个故事讲清楚NIO
    Java并发编程:线程池的使用
    Java垃圾回收机制
    ubuntu sublime text 2 破解版
  • 原文地址:https://www.cnblogs.com/liulangye/p/2606707.html
Copyright © 2011-2022 走看看