Codeforces Round #515 (Div. 3) C (模拟)

题意:

三种操作:

L id ,将编号为id的书放在最左边;

R id,将编号为id的书放在最右边;

?id,查询编号为id的书左右两边哪一边的数量最小,输出最小值。

思路:

用l和r表示下一个左或右的位置,然后模拟就行了。

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=200005;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll qpow(ll a,ll b){ll t=1;while(b){if(b%2){t=(t*a)%mod;b--;}a=(a*a)%mod;b/=2;}return t;}
int main()
{
    std::ios::sync_with_stdio(false);
    int n,l=0,r=1,b[maxn];
    cin>>n;
    while(n--)
    {
        char c;
        int a;
        cin>>c>>a;
        if(c=='L')
        {
            b[a]=l--;
        }
        else if(c=='R')
        {
            b[a]=r++;
        }
        else
        {
            int ln=0,rn=0;
            int ans=min(b[a]-l-1,r-b[a]-1);
            cout<<ans<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Dilly__dally/article/details/83064283