日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Codeforces 1066 C(思维)

發(fā)布時間:2023/12/9 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces 1066 C(思维) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

傳送門:

題面:

C. Books Queries

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

You are given?qq?queries of three types:

  • L?idid?— put a book having index?idid?on the shelf to the left from the leftmost existing book;
  • R?idid?— put a book having index?idid?on the shelf to the right from the rightmost existing book;
  • ??idid?— 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?idid?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?33?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?idids don't repeat in queries of first two types.

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

    Note that after answering the query of type?33?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?qq?(1≤q≤2?1051≤q≤2?105) — the number of queries.

    Then?qq?lines follow. The?ii-th line contains the?ii-th query in format as in the problem statement. It is guaranteed that queries are always valid (for query type?33, 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?33?in the input.

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

    Output

    Print answers to queries of the type?33?in order they appear in the input.

    Examples

    input

    Copy

    8 L 1 R 2 R 3 ? 2 L 4 ? 1 L 5 ? 1

    output

    Copy

    1 1 2

    input

    Copy

    10 L 100 R 100000 R 123 L 101 ? 123 L 10 R 115 ? 100 R 110 ? 115

    output

    Copy

    0 2 1

    Note

    Let's take a look at the first example and let's consider queries:

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

  • The shelf will look like?[100][100];
  • The shelf will look like?[100,100000][100,100000];
  • The shelf will look like?[100,100000,123][100,100000,123];
  • The shelf will look like?[101,100,100000,123][101,100,100000,123];
  • The shelf looks like?[101,100,100000,123][101,100,100000,123]?so the answer is?00;
  • The shelf will look like?[10,101,100,100000,123][10,101,100,100000,123];
  • The shelf will look like?[10,101,100,100000,123,115][10,101,100,100000,123,115];
  • The shelf looks like?[10,101,100,100000,123,115][10,101,100,100000,123,115]?so the answer is?22;
  • The shelf will look like?[10,101,100,100000,123,115,110][10,101,100,100000,123,115,110];
  • The shelf looks like?[10,101,100,100000,123,115,110][10,101,100,100000,123,115,110]?so the answer is?11.
  • 題意:

    ? ? 有三種操作:(1)‘L’,num,將num放入最左端(2)‘R’,num,(3)‘?’,num,讓你從求出最小從左邊或右邊去除多少個數(shù)才能取得數(shù)num。

    題目分析:

    ? ? 最開始想得特別復(fù)雜,認為需要模擬一個動態(tài)向兩邊拓展的數(shù)組,并在每一次操作(1)(2)后用線段樹對現(xiàn)在的整個區(qū)間的[L,R]+1,最后用map記錄一下最小的在哪里就好了。

    ? ? 但是看到一堆人過了這個題后才意識到,題目顯然沒有這么復(fù)雜!!!不知道為什么會想到用所謂的線段樹

    ? ??考慮到要用最小的代價將物品取出,故對于某一個物品,必定是在“?”詢問前的最后一次放入的才是最優(yōu)位置。因此我們只需要用數(shù)組對每個數(shù)記錄一下最優(yōu)解,最后分別用取(最優(yōu)位置-最左端位置,以及最右端-最優(yōu)位置)中的最小值即可。

    代碼:

    #include <bits/stdc++.h> #define maxn 400005 using namespace std; int a[maxn]; char str[2]; int main() {int t;scanf("%d",&t);int l=200000;int r=l-1;while(t--){int num;scanf("%s",str);scanf("%d",&num);if(str[0]=='L'){l--;a[num]=l;}else if(str[0]=='R'){r++;a[num]=r;}else{int res=min(a[num]-l,r-a[num]);cout<<res<<endl;}}return 0; }

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/Chen-Jr/p/11007168.html

    創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

    總結(jié)

    以上是生活随笔為你收集整理的Codeforces 1066 C(思维)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。