Books Queries(codeforces 1066)

You have got a shelf and want to put some books on it.

You are given q queries of three types:

L
id — put a book having index id on the shelf to the left from the leftmost existing book;
R id — put a book having index id on the shelf to the right from the rightmost existing book;
? id — calculate the minimum number of books you need to pop from the left or from the right in such a way that the book with index id will be leftmost or rightmost.
You can assume that the first book you will put can have any position (it does not matter) and queries of type 3 are always valid (it is guaranteed that the book in each such query is already placed). You can also assume that you don’t put the same book on the shelf twice, so ids don’t repeat in queries of first two types.

Your problem is to answer all the queries of type 3 in order they appear in the input.

Note that after answering the query of type 3 all the books remain on the shelf and the relative order of books does not change.

If you are Python programmer, consider using PyPy instead of Python when you submit your code.

Input
The first line of the input contains one integer (1≤q≤2⋅105) — the number of queries.

Then q lines follow. The i-th line contains the i-th query in format as in the problem statement. It is guaranteed that queries are always valid (for query type 3, it is guaranteed that the book in each such query is already placed, and for other types, it is guaranteed that the book was not placed before).

It is guaranteed that there is at least one query of type 3 in the input.

In each query the constraint 1≤id≤2⋅105 is met.

Output
Print answers to queries of the type。3 in order they appear in the input.

Examples
inputCopy
8
L 1
R 2
R 3
? 2
L 4
? 1
L 5
? 1
outputCopy
1
1
2
inputCopy
10
L 100
R 100000
R 123
L 101
? 123
L 10
R 115
? 100
R 110
? 115
outputCopy
0
2
1
Note
Let’s take a look at the first example and let’s consider queries:

The shelf will look like [1];
The shelf will look like [1,2];
The shelf will look like [1,2,3];
The shelf looks like [1,2,3] so the answer is 1;
The shelf will look like [4,1,2,3];
The shelf looks like [4,1,2,3] so the answer is 1;
The shelf will look like [5,4,1,2,3];
The shelf looks like [5,4,1,2,3] so the answer is 2.
Let’s take a look at the second example and let’s consider queries:

The shelf will look like [100];
The shelf will look like [100,100000];
The shelf will look like [100,100000,123];
The shelf will look like [101,100,100000,123];
The shelf looks like [101,100,100000,123] so the answer is 0;
The shelf will look like [10,101,100,100000,123];
The shelf will look is[10,101,100,100000,123,115];
The shelf looks like [10,101,100,100000,123,115] so the answer is 2;
The shelf will look like [10,101,100,100000,123,115,110];
The shelf looks like [10,101,100,100000,123,115,110] so the answer is 1.

虽然是c题了,但是感觉还是没有那么难,比第二个简单。。
就是按着左或者右来放书,?的时候就查寻从左或者从右最小的位置。用两个数标记左右的位置就行了。
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxx=2e5+10;
int a[maxx];

int main()
{
	int q;
	char s;
	int p;
	while(cin>>q)
	{
		int l=1;
		int r=0;
		for(int i=0;i<q;i++)
		{
			cin>>s>>p;
			if(s=='L') a[p]=--l;
			else if(s=='R') a[p]=++r;
			else cout<<min(a[p]-l,r-a[p])<<endl;
		}
	}
}

努力加油a啊,(o)/~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值