CF1066C题解
Solution
首先考虑双端队列,但是 \(O(n)\) 的复杂度会超时,干脆接用数组模拟。
过程如下:
- 定义两个变量 \(L\) 和 \(R\),分别表示左右端点,\(L\) 和 \(R\) 初始化都为最大值。
- 如果左侧插入左端点向左移一位,右侧插入右端点向右移一位。
- 定义一个数组 \(site\) 用来储存每个 \(id\) 的下标。
- 重点,第一次输入的 \(site_{id}\) 为最大值,给左边预留出充足的空间。
code
#include <bits/stdc++.h>
using namespace std;
const int maxn=100005;
int m,site[2*maxn],L=maxn,R=maxn;
int main()
{
std::ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);//优化输入输出
cin>>m;
for(int i=0; i<m; i++)
{
char s;
int id;
cin>>s>>id;
if(!i)// 第一个点的位置是maxn
{
site[id]=maxn;continue;
}
if(s=='L')
site[id]=--L;
else
f(s=='R')
site[id]=++R;
else
cout<<min(R-site[id],site[id]-L)<<endl;//取前后最小值
}
return 0;
}
完结撒花✿✿ヽ(°▽°)ノ✿
work by Simon

浙公网安备 33010602011771号