当前位置:网站首页>HDU - 7072 double ended queue + opposite top

HDU - 7072 double ended queue + opposite top

2022-06-23 01:24:00 Lupin123123

Topic link
Top two end queue .
Insert from the left to the left deque in , Insert from the right to the right deque in , And maintain in the process lsize=rsize or lsize+1=rsize.
tips: This treatment of the top is in This question It also uses .

#include<bits/stdc++.h>
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define INF 0x3f3f3f3f
typedef long long ll;
const int maxn = 1e7+5;

using namespace std;

deque<int> lef,rig;
int n,x,lsize,rsize;
int bel[maxn],del[maxn];
char opt[2];

void work()
{
    
	while(lsize!=rsize && lsize+1!=rsize)
	{
    
		if (lsize<rsize && lsize+1<rsize)
		{
    
			int tmp=rig.front();
			if (del[tmp])
			{
    
				rig.pop_front();
				continue;
			}
			rig.pop_front();
			lef.push_back(tmp);	
			bel[tmp]=0;
			rsize--, lsize++;
		}
		else if (lsize>rsize && lsize+1>rsize)
		{
    
			int tmp=lef.back();
			if (del[tmp])
			{
    
				lef.pop_back();
				continue;
			}
			lef.pop_back();
			rig.push_front(tmp);		
			bel[tmp]=1;
			rsize++, lsize--;
		}	
	}	
}

//void print()
//{
    
// printf("***\n");
// for (deque<int> ::iterator it=lef.begin(); it!=lef.end(); it++) printf("%d ",*it);
// printf("\n");
// for (deque<int> ::iterator it=rig.begin(); it!=rig.end(); it++) printf("%d ",*it);
// printf("\n");
// for (deque<int> ::iterator it=lef.begin(); it!=lef.end(); it++) printf("%d ",*it);
// for (deque<int> ::iterator it=rig.begin(); it!=rig.end(); it++) printf("%d ",*it);
// printf("\n***\n");
//}

int main()
{
    
	while(~scanf("%d",&n))
	{
    
		x=0;
		while(!lef.empty()) lef.pop_front();
		while(!rig.empty()) rig.pop_front();
		
		for (int i=1; i<=n; i++)
		{
    
			scanf("%s",opt);
			if (opt[0]=='L')
			{
    
				++x;
				lef.push_front(x);
				bel[x]=0;
				lsize++;
				work();
			}
			else if (opt[0]=='R')
			{
    
				++x;
				rig.push_back(x);
				bel[x]=1;
				rsize++;
				work();
			}
			else if (opt[0]=='G')
			{
    
				int num;
				scanf("%d",&num);
				del[num]=1;
				if (bel[num]==0) lsize--;
				else rsize--;
				work();
			}
			else
			{
    
				int ans;
				while(del[rig.front()]) rig.pop_front();
				ans=rig.front();
				printf("%d\n",ans);
			}
// print();
		}
	}
return 0;
}


原网站

版权声明
本文为[Lupin123123]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220917474994.html