zoukankan      html  css  js  c++  java
  • splay复习

    The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped with a modern computing environment provided by IBM Romania, and using modern information technologies. As usual, each client of the bank is identified by a positive integer K and, upon arriving to the bank for some services, he or she receives a positive integer priority P. One of the inventions of the young managers of the bank shocked the software engineer of the serving system. They proposed to break the tradition by sometimes calling the serving desk with thelowest priority instead of that with the highest priority. Thus, the system will receive the following types of request:

    0 The system needs to stop serving
    KP Add client K to the waiting list with priority P
    2 Serve the client with the highest priority and drop him or her from the waiting list
    3 Serve the client with the lowest priority and drop him or her from the waiting list

    Your task is to help the software engineer of the bank by writing a program to implement the requested serving policy.

    Input

    Each line of the input contains one of the possible requests; only the last line contains the stop-request (code 0). You may assume that when there is a request to include a new client in the list (code 1), there is no other request in the list of the same client or with the same priority. An identifier K is always less than 106, and a priority P is less than 107. The client may arrive for being served multiple times, and each time may obtain a different priority.

    Output

    For each request with code 2 or 3, the program has to print, in a separate line of the standard output, the identifier of the served client. If the request arrives when the waiting list is empty, then the program prints zero (0) to the output.

    Sample Input
    2
    1 20 14
    1 30 3
    2
    1 10 99
    3
    2
    2
    0
    Sample Output
    0
    20
    30
    10
    0
    题意百度吧,先用set水过了
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<set>
    #include<map>
    using namespace std;
    map<int,int> Map;
    int main()
    {
        set<int> s;
        s.clear();
        set<int>::iterator it1;
        int Mode;
        while(scanf("%d",&Mode)&&Mode)
        {
            //cout<<"Mode"<<Mode<<endl;
            if(Mode==2)
            {
                
                if(s.empty())
                {
                    cout<<0<<endl;
                    continue;
                }
                it1=s.end();
                it1--;
                cout<<Map[*it1]<<endl;
                s.erase(it1);
            }
            if(Mode==3)
            {
                if(s.empty())
                {
                    cout<<0<<endl;
                    continue;
                }
                it1=s.begin();
                cout<<Map[*it1]<<endl;
                s.erase(it1);
            }
            if(Mode==1)
            {
                int p,q;
                scanf("%d %d",&p,&q);
                //cout<<"PQ"<<p<<' '<<q<<endl;
                s.insert(q);
                Map[q]=p;
            }
        }
     } 

    然后复习了一下splay,rotate背不出啊

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int c[100000][2],n,a[1000005],b[1000005],pre[1000005];
    void update(int u,int fa)
    {
        if(b[u]>b[fa])
        {
            if(c[fa][1]) update(u,c[fa][1]);else
            {
                c[fa][1]=u;
                pre[u]=fa;
            }
        }else
            if(c[fa][0]) update(u,c[fa][0]);else
            {
                c[fa][0]=u;
                pre[u]=fa;
            }
    }
    void debug(int u)
    {
        if(c[u][0]) debug(c[u][0]);
        cout<<u<<' '<<b[u]<<endl;
        if(c[u][1]) debug(c[u][1]);
    }
    int tree_max(int x)
    {
        while(c[x][1]) x=c[x][1];
        return x;
    }
    int tree_min(int x)
    {
        while(c[x][0]) x=c[x][0];
        return x;
    }
    void rotate(int x,int &k)
    {
        int y=pre[x],z=pre[y],l,r;
        if(c[y][0]==x) l=0;else l=1;r=l^1;
        if(y==k) k=x;
        else{
            if(c[z][0]==y) c[z][0]=x;else c[z][1]=x;
        }
        pre[x]=z;pre[y]=x;pre[c[x][r]]=y;
        c[y][l]=c[x][r];c[x][r]=y;
    }
    void splay(int x,int &k)
    {
        while(x!=k)
        {
            int y=pre[x],z=pre[y];
            if(y!=k)
            {
                if(c[y][0]==x^c[z][0]==y)
                {
                    rotate(x,k);
                }
                rotate(y,k);
            }
            rotate(x,k);
        }
    }
    int main()
    {
        int Mode,rt=0,p,q;
        while(scanf("%d",&Mode)&&Mode)
        {
            if(Mode==1)
            {
                n++;
                scanf("%d %d",&a[n],&b[n]);
                if(rt==0)
                {
                    rt=n;
                }else
                {
                    update(n,rt);
                }
                continue;
            }
            int pq=tree_max(rt),pp=tree_min(rt);
            if(Mode==2)
            {
                cout<<a[pq]<<endl;
                if(pq!=rt) splay(pq,rt);
                rt=c[pq][0];
                pre[pq]=pre[c[pq][0]]=0;
                c[pq][0]=0,c[pq][1]=0;
            }else if(Mode==3)
            {
                cout<<a[pp]<<endl;
                if(pp!=rt) splay(pp,rt);
                rt=c[pp][1];
                pre[pp]=pre[c[pp][1]]=0;
                c[pp][0]=0,c[pp][1]=0;
            }
        }
        //debug(rt);
    }

    这里好像用了一些离散化的技巧,没有直接存关键值,而是映射到编号那边

  • 相关阅读:
    sql 一对多变成一对多的最后一条记录的做法
    2.如何优化操作大数据量数据库(改善SQL语句)
    asp数据操作类DB
    4.如何优化操作大数据量数据库(几十万以上数据)(如何选择聚合索引)
    【转】asp中记录集对象的getrows和getstring用法
    用DB类写新闻系统1
    Apache+Tomcat配置负载均衡
    C# CAD 几何图形周围创建尽可能小的圆 使用 .NET 在 2D AutoCAD
    C# Cad 2007 工具栏 工具条创建
    C# Cad 拉伸 关键 GetStretchPoints MoveStretchPointsAt
  • 原文地址:https://www.cnblogs.com/dancer16/p/7340347.html
Copyright © 2011-2022 走看看