sgu271 bookpile

http://acm.sgu.ru/problem.php?contest=0&problem=271

n本书支持最上k个反转和加入操作,问最后的结果。

这道题让我想起多校的那道可合并栈,

那道题有两种做法,一种是再开一个栈,合并的时候修改指针并不真的合并,另一种是我用的左偏树。

同样的这题也是,一种做法是利用双端队列,反转并不真的反转,而是改变pop方向,同时再开一个dequeue,

只保留k个数据,另一种做法是splay树。

迷的是手动写deque wa到死,但是stl很好理解,push_front就是堆的增长,pop_back就是堆的k淘汰

 

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<string>
#include<cctype>
#include<queue>
#include<set>
#include<sstream>
#include<map>
using namespace std;
#define FORD(i,k,n) for(int i=n;i>=k;i--)
#define FOR(i,k,n) for(int i=k;i<=n;i++)
#define CLR(a) memset(a,0,sizeof(a));
#define NEG(a) memset(a,-1,sizeof(a));
#define FILL(a) memset(a,0x3f,sizeof(a));
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define ll long long
#define LL long long
#define i64 long long
#define u32 unsigned int
#define u64 unsigned long long
#define ptb(b,a){int tmp=a;string s;do{s+=tmp%2+'0';tmp/=2;}while(tmp);reverse(s.begin(),s.end());cout<<"bin "<<b<<"="<<s<<endl;}
#define pta(i,a,f,b) {FOR(i,f,b) cout<<a[i]<<" "; printf("\n");}
#define flgpt(flg,a,b) {if(flg) printf("%s\n",a);else printf("%s\n",b);}
#define pt(a) cout<<a<<endl
#define pt2(a,b) cout<<a<<" "<<b<<endl
#define pt3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
#define pt4(a,b,c,d) cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl
#define ptl cout<<"-------------"<<endl
#define ptl2 cout<<"~~~~~~~~~~~~~~~~"<<endl
#define fp   freopen("in.txt","r",stdin)
#define maxn 1000000


int N,M,K;
deque<string>q;
deque<string>rem;
int main()
{

    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif
    cin>>N>>M>>K;
    FOR(i,1,N)
    {
        string s;
        cin>>s;
        if(q.size()<K)
            q.push_back(s);
        else
            rem.push_back(s);
    }
    // cout<<q.front()<<" "<<q.back()<<endl;
    int flg=1;
    FOR(i,1,M)
    {
        string s;
        cin>>s;
        if(s[0]=='A')
        {
            int ptr=0,ok=0;
            string name;
            while(ptr<s.size())
            {
                if(ok&&s[ptr]!=')')
                {
                    name+=s[ptr];
                }
                if(s[ptr]=='(') ok=1;
                ptr++;
            }
            s=name;

            if(flg)
            {
                if(q.size()<K)
                    q.push_front(s);
                else
                {
                    q.push_front(s);
                    rem.push_front(q.back());
                    q.pop_back();
                    // cout<<q.front()<<" "<<q.back()<<endl;
                }
            }
            else
            {
                if(q.size()<K)
                    q.push_back(s);
                else
                {
                    q.push_back(s);
                    rem.push_front(q.front());
                    // cout<<q.front()<<" "<<q.back()<<endl;
                    q.pop_front();
                    // cout<<q.front()<<":"<<q.back()<<endl;
                }
            }
        }
        else
        {
            flg=1-flg;
        }
        /*FOR(i,Q.s,Q.t-1) cout<<Q.q[i]<<" ";
        puts("");*/
    }
    if(flg)
    while(!q.empty())
    {
        cout<<q.front()<<endl;
        q.pop_front();
    }
    else 
    while(!q.empty())
    {
        cout<<q.back()<<endl;
        q.pop_back();
    }
    while(!rem.empty())
    {
        cout<<rem.front()<<endl;
        rem.pop_front();
    }
    return 0;
}
View Code

 

posted @ 2016-09-03 14:12  aidgn  阅读(146)  评论(0编辑  收藏  举报