3403: [usaco 2009 Open] cow line on the ox time limit: 3 sec memory limit: 128 MB
Submit: 48 solved: 41
[Submit] [Status] Description Description John's n cows (numbered 1 to n) are queuing in a straight line. at the beginning of a straight line, there was no cow. next, an event occurs (1 ≤ S ≤ 100000). One event may be one of the following four situations :. enter "Al" on the left of the team for a cow ").. enter "Ar" on the right side of a cow's team "). · K cows left the team (enter "dlk "). · K. The cows on the right of the team leave (enter "DRK "). what is the final team requested. the data ensures that the number of cows in the team will not exceed the number of cows in the team. The final team does not enter 1st rows of input s, and then each line of S describes an event, the format is shown in the topic description. output is the final result of the output team from left to right. sample input10
A L
A L
A R
A L
D r 2
A R
A R
D l 1
A L
A rsample output7
2
5
6
8 hint
Source
Silver
Problem: Hehe, the complexity of brute-force balancing is also O (n) code :( 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 << endl18 #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
Bzoj3403: [usaco 2009 Open] cow line on the ox