【BZOJ】3403: [Usaco2009 Open]Cow Line 直线上的牛(模拟)

http://www.lydsy.com/JudgeOnline/problem.php?id=3404

裸的双端队列。。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

const int N=100005;
int q[N], n, front, tail;

inline void fix(int &x) { if(x<0) x=N+x; if(x>=N) x-=N; }
int main() {
    read(n);
    int cnt=0;
    for1(i, 1, n) {
        char ch=getchar(); while(ch<'A'||ch>'Z') ch=getchar();
        if(ch=='A') {
            ch=getchar(); while(ch<'A'||ch>'Z') ch=getchar();
            if(ch=='L') { --front; fix(front); q[front]=++cnt; }
            else if(ch=='R') q[tail++]=++cnt, fix(tail);
        }
        else if(ch=='D') {
            ch=getchar(); while(ch<'A'||ch>'Z') ch=getchar();
            int t=getint();
            if(ch=='L') front+=t, fix(front);
            else if(ch=='R') tail-=t, fix(tail);
        }
    }
    while(front!=tail) {
        printf("%d
", q[front++]); fix(front);
    }
    return 0;
}

Description

题目描述
    约翰的N只奶牛(编为1到N号)正在直线上排队.直线上开始的时候一只牛也没有.接下来发生了S(1≤S≤100000)次事件,一次事件可能是以下四种情况之一:
  .一只奶牛加入队伍的左边(输入“AL”).
  .一只奶牛加入队伍的右边(输入“AR”).
  ·K只队伍左边奶牛离开(输入“DLK”).
  ·K只队伍右边奶牛离开(输入“DRK”).
    请求出最后的队伍是什么样.
    数据保证离开的奶牛不会超过队伍里的奶牛数,最后的队伍不空

Input

    第1行输入S,之后S行每行描述一次事件,格式如题目描述所示

Output

 
    由左到右输出队伍最后的情况.

Sample Input

10
A L
A L
A R
A L
D R 2
A R
A R
D L 1
A L
A R

Sample Output

7
2
5
6
8

HINT

Source

原文地址:https://www.cnblogs.com/iwtwiioi/p/3971328.html