Codeforces Round #515 (Div. 3) C (模擬)
阿新 • • 發佈:2018-11-08
題意:
三種操作:
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; }