• CodeForces Round #515 Div.3 C. Books Queries


    http://codeforces.com/contest/1066/problem/C

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

    You are given qq queries of three types:

    1. idid — put a book having index idid on the shelf to the left from the leftmost existing book;
    2. idid — put a book having index idid on the shelf to the right from the rightmost existing book;
    3. 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 (1q21051≤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 1id21051≤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:

    1. The shelf will look like [1][1];
    2. The shelf will look like [1,2][1,2];
    3. The shelf will look like [1,2,3][1,2,3];
    4. The shelf looks like [1,2,3][1,2,3] so the answer is 11;
    5. The shelf will look like [4,1,2,3][4,1,2,3];
    6. The shelf looks like [4,1,2,3][4,1,2,3] so the answer is 11;
    7. The shelf will look like [5,4,1,2,3][5,4,1,2,3];
    8. 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:

    1. The shelf will look like [100][100];
    2. The shelf will look like [100,100000][100,100000];
    3. The shelf will look like [100,100000,123][100,100000,123];
    4. The shelf will look like [101,100,100000,123][101,100,100000,123];
    5. The shelf looks like [101,100,100000,123][101,100,100000,123] so the answer is 00;
    6. The shelf will look like [10,101,100,100000,123][10,101,100,100000,123];
    7. The shelf will look like [10,101,100,100000,123,115][10,101,100,100000,123,115];
    8. The shelf looks like [10,101,100,100000,123,115][10,101,100,100000,123,115] so the answer is 22;
    9. The shelf will look like [10,101,100,100000,123,115,110][10,101,100,100000,123,115,110];
    10. The shelf looks like [10,101,100,100000,123,115,110][10,101,100,100000,123,115,110] so the answer is 11.

    代码:

    #include <iostream>
    #include <cstring>
    #include <stdio.h>
    using namespace std;
    
    int a[200005];
    
    int main() {
        int q;
        while (~scanf("%d", &q)) {
            char op[2];
            memset(a, 0, sizeof(a));
            int x, l = 100000, r = 100000;
            for (int i = 0; i < q; i ++) {
                scanf("%s%d", op, &x);
                if (strcmp(op, "L") == 0) {
                    a[x] = l --;
                    if (i == 0) r ++;
                } else if (strcmp(op, "R") == 0) {
                    a[x] = r ++;
                    if (i == 0) l --;
                }
                else
                    printf("%d
    ", min(a[x] - l, r - a[x]) - 1);
    
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    【转】什么是5G?居然有人用漫画把它讲得如此接地气!
    【转】Centos下编译升级安装Boost
    【转】SSH穿越跳板机:一条命令跨越跳板机直接登陆远程计算机
    部署JupyterLab和pyalgotrade搭建web策略回测环境
    [转]Linux中python3.6+ipython+Jupyter Notebook环境
    环境命令备忘
    [转]微软商店 打开就显示无法加载该页面 代码0x80131500?
    [转]Centos 7 安装部署 GitLab 服务器
    [转]本文采用all-in-one(一体化的)安装OpenShift
    [转]Linux编译和安装boost库
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/9807053.html
Copyright © 2020-2023  润新知