BZOJ3403:[USACO2009OPEN]Cow Line

浅谈队列:https://www.cnblogs.com/AKMer/p/10314965.html

题目传送门:https://lydsy.com/JudgeOnline/problem.php?id=3403

直接双端队列模拟即可。

时间复杂度:(O(S))

空间复杂度:(O(S))

代码如下:

#include <cstdio>
using namespace std;

const int maxn=1e5+5;

char s1[5],s2[5];
int list[maxn<<1];
int n,head,tail,cnt;

int read() {
	int x=0,f=1;char ch=getchar();
	for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
	for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
	return x*f;
}

int main() {
	n=read();
	head=tail=maxn;
	while(n--) {
		scanf("%s%s",s1+1,s2+1);
		if(s1[1]=='A') {
			if(s2[1]=='L')list[--head]=++cnt;
			else list[tail++]=++cnt;
		}
		else {
			int k=read();
			if(s2[1]=='L')while(k--)head++;
			else while(k--)tail--;
		}
	}
	for(int i=head;i<tail;i++)
		printf("%d
",list[i]);
	return 0;
}
原文地址:https://www.cnblogs.com/AKMer/p/10325611.html