2021多校杭电Boring data structure problem HDU - 7072(双端队列模拟)

"Why don't you write some background story for this boring data structure problem?" 
"Because it's too boring..." 

There's a queue(not the original queue one may refer to as a data structure, here a double-ended queue, to be more precise) that is initially empty. Then, there are infinite elements numbered 1,2,…1,2,…, entering the queue in the order of their numbers(i.e., the first element to enter the queue is numbered 11, the second, 22 et cetera). If an element leaves the queue, it will not enter the queue anymore. 

Now there are qq operations, each in one of the following forms: 

  • Let the next element enter the left end of the queue.
  • Let the next element enter the right end of the queue.
  • Let the element numbered xx leave the queue.
  • Ask the number of the element in the middle of the queue. (If there are mm elements in the queue currently, you should output the number of the element that is the ⌈m+12⌉⌈m+12⌉th from the left).

Input

The first line of each test case contains one number q(1≤q≤107)q(1≤q≤107), denoting the number of operations. 

Then qq lines follow, each in one of the following forms: 

  • LL, denoting the next element enters the left end of the queue
  • RR, denoting the next element enters the right end of the queue
  • GG xx, denoting the element numbered xx leaves the queue (It is guaranteed that the element numbered xx is in the queue when this operation is applied)
  • QQ denoting a query that asks the number of the element in the middle of the queue(It is guaranteed that the queue is not empty when this operation is applied)


It is guaranteed that the number of operations of the third and the fourth type is both less than 1.5⋅1061.5⋅106.

Output

For each operation of the fourth kind, output a number in a line, denoting the answer to the query.

Sample Input

9
L
L
L
Q
R
Q
G 1
R
Q

Sample Output

2
1
4

思路:

开两个双端队列 rq , lq 分别维护左右两半儿,L从lq左加入 R从rq右端加入,删除不用真删除标记为0即可,每操作完一步,用维护函数维护一下(保证nl==nr或者 nl+1==nr )查询的就是rq的第一个数

#include<string.h>
#include<algorithm>
#include<stdio.h>
#include<deque>
using namespace std;
int nl,nr,book[10100133];
deque <int >  lq,rq;
void wei_hu()
{
	while(nl>nr)
	{
		while(book[lq.back()]==0) lq.pop_back();
		rq.push_front(lq.back());
		lq.pop_back();
		nl--,nr++;
		book[rq.front()]=2;
	}
	while((nl+1)<nr)
	{
		while(book[rq.front()]==0) rq.pop_front();
		lq.push_back(rq.front());
		rq.pop_front();
		nl++,nr--;
		book[lq.back()]=1;
	}
}

int main()
{
	int n,m,j,i,w,z=0;
	char a[2];
	scanf("%d",&w);
	z=nl=nr=0;
	while(w--)
	{
		scanf("%s",a);
		if(a[0]=='L')
		{
			lq.push_front(++z);
			nl++;
			book[z]=1;
			wei_hu();
		}
		if(a[0]=='R')
		{
			rq.push_back(++z);
			nr++;
			book[z]=2;
			wei_hu();
		}
		if(a[0]=='G')
		{
			scanf("%d",&n);
			if(book[n]==1) nl--;
			if(book[n]==2) nr--;
			book[n]=0;
			wei_hu();
		}
		if(a[0]=='Q')
		{
			while(book[rq.front()]==0) rq.pop_front();
			printf("%d\n",rq.front());
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值