思路
一开始都让left1=right1=300000
然后x存到左边就记录一下loc[x]=–left1,如果x存到右边就记录一下loc[x]=++right1
如果要查询的话就输出min(abs(loc[x]-right1),abs(loc[x]-left1))
代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 600000;
int a[N],loc[N];
int left1=300000,right1=300000;
int n;
int main()
{
cin>>n;
char c;
int x;
cin>>c>>x;
a[left1]=x;
loc[x]=300000;
n--;
while (n -- )
{
cin>>c>>x;
if(c=='L')
{
loc[x]=--left1;
}
else if(c=='R')
{
loc[x]=++right1;
}
else if(c=='?')
{
cout<<min(abs(loc[x]-right1),abs(loc[x]-left1))<<endl;
}
}
return 0;
}
李知恩
只需要存下标,秒啊
我一开始在那边数组模拟双链表,双端队列嗯做怎么都做不出,后来一想,完全没必要真的存,想了好长时间一直在想怎么优化时间😂