BZOJ3403: [Usaco2009 Open]Cow Line 直线上的牛

3403: [Usaco2009 Open]Cow Line 直线上的牛

Time Limit: 3 Sec  Memory Limit: 128 MB
Submit: 48  Solved: 41
[Submit][Status]

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

题解:
呵呵,暴力均摊复杂度也是O(n)的
代码:(copy)
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <string>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <queue>
 8 using namespace std;
 9 #define rep(i, n) for(int i=0; i<(n); ++i)
10 #define for1(i,a,n) for(int i=(a);i<=(n);++i)
11 #define for2(i,a,n) for(int i=(a);i<(n);++i)
12 #define for3(i,a,n) for(int i=(a);i>=(n);--i)
13 #define for4(i,a,n) for(int i=(a);i>(n);--i)
14 #define CC(i,a) memset(i,a,sizeof(i))
15 #define read(a) a=getint()
16 #define print(a) printf("%d", a)
17 #define dbg(x) cout << #x << " = " << x << endl
18 #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
19 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; }
20 inline const int max(const int &a, const int &b) { return a>b?a:b; }
21 inline const int min(const int &a, const int &b) { return a<b?a:b; }
22  
23 const int N=100005;
24 int q[N], n, front, tail;
25  
26 inline void fix(int &x) { if(x<0) x=N+x; if(x>=N) x-=N; }
27 int main() {
28     read(n);
29     int cnt=0;
30     for1(i, 1, n) {
31         char ch=getchar(); while(ch<'A'||ch>'Z') ch=getchar();
32         if(ch=='A') {
33             ch=getchar(); while(ch<'A'||ch>'Z') ch=getchar();
34             if(ch=='L') { --front; fix(front); q[front]=++cnt; }
35             else if(ch=='R') q[tail++]=++cnt, fix(tail);
36         }
37         else if(ch=='D') {
38             ch=getchar(); while(ch<'A'||ch>'Z') ch=getchar();
39             int t=getint();
40             if(ch=='L') front+=t, fix(front);
41             else if(ch=='R') tail-=t, fix(tail);
42         }
43     }
44     while(front!=tail) {
45         printf("%d\n", q[front++]); fix(front);
46     }
47     return 0;
48 }
View Code

 

posted @ 2014-10-05 09:00  ZYF-ZYF  Views(233)  Comments(0Edit  收藏  举报