数组模拟双向链表-肝了两天

题目链接

  • 代码

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N=10000015,mod=1e9+7;
template<typename T>
void Debug(T x,string s){
    cout<<s<<": "<<x<<endl;
}
struct node
{
	int val,p_cnt;
	int next,last;
}a[N];

#define PII pair<int,int>
// #define x first
// #define y second
#define PB push_back
int vis[N],vis_p[N];//value值在链表中的位置,value值对应的数组下标
struct linklist
{
	int head=1,tail=1,sz=0,mid=tail;
	void insertl(int tot){sz++;
		if(sz==1){
			head=tail=tot;vis_p[tot]=0;
			a[tot].last=-1;a[tot].next=-1;
			mid=tail;
		}
		else{
			a[tot].next=head;
			a[tot].last=-1;
			a[head].last=tot;
			vis_p[tot]=vis_p[head]-1;
			head=tot;
			if(sz%2==1){
				movel();
			}
			else{
				mid=mid;
			}
		}
		
	}
	void insertr(int tot){
		sz++;
		if(sz==1){
			head=tail=tot;vis_p[tot]=0;
			a[tot].last=-1;a[tot].next=-1;
			mid=tot;
		}
		else{
			a[tot].last=tail;
			a[tot].next=-1;
			a[tail].next=tot;
			vis_p[tot]=vis_p[tail]+1;
			tail=tot;
			if(sz%2==0){
				mover();
			}
			else mid=mid;
		}
		
	}
	void del(int tot){
		int tl=a[tot].last,tr=a[tot].next;
		if(tr!=-1)
		a[tl].next=tr;
		if(tl!=-1)
		a[tr].last=tl;
		if(tot==head){	
			head=a[head].next;
		}
		if(tot==tail){
			tail=a[tail].last;
		}
			if(vis_p[mid]>vis_p[tot]){
				if(sz%2==1) mover();
				else mid=mid;
			}
			else if(vis_p[mid]<vis_p[tot]){
				if(sz%2==0) movel();
				else mid=mid;
			}
			else if(mid==tot){
				if(sz%2==0) movel();
				else mover();
			}
		sz--;
		a[tot].last=a[tot].next=-1;
	}
	void movel(){
		mid=a[mid].last;
	}
	void mover(){
		mid=a[mid].next;
	}
}List;
int tot=0;
void solve()
{
	int posl=0,posr=0;a[0].last=a[0].next=-1;
	int q;scanf("%d ",&q);char c;
	for(int i=1;i<=q;i++){
		scanf("%c ",&c);
		// cout<<c<<endl;
		if(c=='L'){++tot;
			List.insertl(tot);
		}
		else if(c=='R'){++tot;
			List.insertr(tot);
		}
		else if(c=='G'){
			int x;scanf("%d ",&x);
			List.del(x);
		}
		else{
			printf("%d\n",List.mid);
		}
	}
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("b.txt", "r", stdin);
	freopen("bout.txt", "w", stdout);
#endif
// #ifndef ONLINE_JUDGE
// 	freopen("a.txt", "r", stdin);
// 	freopen("aout.txt", "w", stdout);
// #endif
	int t;
	t=1;
	while (t--)
		solve();
}
  • 呜呜呜,花了两天终于过了!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯狂的码泰君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值