zoukankan      html  css  js  c++  java
  • 2018百度之星初赛A轮 度度熊学队列

    注意:刚开始用数组存deque<int> qa[MAX]会爆内存

    需要改用map<int, deque<int> > qa优化

    不明觉厉

    #include<bits/stdc++.h>
    using namespace std;
    void read(int &x){
        char ch = getchar();x = 0;
        for (; ch < '0' || ch > '9'; ch = getchar());
        for (; ch >='0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
    }

    map<int,deque<int> >qa;
    int N,Q;
    int main()
    {
        //freopen("in.txt","r",stdin);
        int sign,u,v,w,val;
        while(cin>>N>>Q)
        {
            qa.clear();
        while(Q--)
        {
            read(sign);
            if(sign==1)
            {
                read(u);read(w);read(val);
                if(w==0){

                        qa[u].push_front(val);
                }
                else qa[u].push_back(val);
            }
            if(sign==2)
            {
                read(u);read(w);
                if(qa[u].empty())
                        {
                            cout<<-1<<endl;
                            continue;
                        }
                if(w==0){cout<<qa[u].front()<<endl;qa[u].pop_front();}
                else {cout<<qa[u].back()<<endl;qa[u].pop_back();}
            }
            if(sign==3)
            {
                read(u);read(v);read(w);
                if(w==0)
                {

                    qa[u].insert(qa[u].end(),qa[v].begin(),qa[v].end());
                }
                else
                {
                    qa[u].insert(qa[u].end(),qa[v].rbegin(),qa[v].rend());
                }
                qa[v].clear();
            }
        }
        }
    }
     

  • 相关阅读:
    AGC044D Guess the Password
    CF1290E Cartesian Tree
    loj2537. 「PKUWC2018」Minimax
    loj3166. 「CEOI2019」魔法树
    CF702F T-Shirts
    CF1260F Colored Tree
    CF1340F Nastya and CBS
    CF1017G The Tree
    CF150E Freezing with Style
    前端开发 -- HTML
  • 原文地址:https://www.cnblogs.com/linruier/p/9485159.html
Copyright © 2011-2022 走看看