AcWing 3548. 双端队列
原题链接
中等
#include<bits/stdc++.h>
using namespace std;
char f[200010];
int x[200010];
int main(){
unordered_map<int,int> m;
int q;
cin>>q;
int L = 0, R = 0;
cin>>f[0]>>x[0];
m[x[0]] = 0;
for(int i = 1; i < q; i++){
cin>>f[i]>>x[i];
if(f[i] == 'L') L --, m[x[i]] = L;
else if(f[i] == 'R') R ++, m[x[i]] = R;
}
// for(auto [a,b] : m){
// cout<<a<<" "<<b<<endl;
// }
int l = 0, r = 0;
for(int i = 1; i < q; i++){
if(f[i] == 'L') l --;
else if(f[i] == 'R') r ++;
else {
// cout<<m[x[i]]<<endl;
cout<<min(m[x[i]] - l,r - m[x[i]])<<endl;
}
}
return 0;
}