Codeforces 1066 C(思維)

傳送門:

題面:

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:

  1. L?idid?— put a book having index?idid?on the shelf to the left from the leftmost existing book;
  2. R?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?(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:

  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.

題意:

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

題目分析:

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

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

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

代碼:

#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;
}

?

轉載于:https://www.cnblogs.com/Chen-Jr/p/11007168.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278959.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278959.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278959.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

outlook默認簽名設置_如何將默認簽名添加到Outlook會議請求

outlook默認簽名設置An odd quirk in Outlook is the inability to add a default signature to meeting requests. Here’s a quick and simple way to set up a one-click solution that avoids cutting and pasting every time you create a meeting. Outlook中的一個奇怪問…

技嘉 linux設置u盤啟動項,技嘉主板bios設置u盤啟動教程

對于想要重裝系統的朋友來說&#xff0c;進bios一直是最大的難關&#xff0c;對于技嘉主板來說尤為復雜&#xff0c;下面小編就詳細給大家介紹一下技嘉主板bios設置u盤啟動的方法。方法一&#xff1a;使用u盤啟動快捷鍵直接進入u盤裝系統1、技嘉主板u盤啟動快捷鍵是F12&#xf…

uefi模式下win10安裝雙系統ubuntu18.04LTS

自己折騰了半天&#xff0c;血與淚啊&#xff08;難得一個可愛的周末 wwww我一定要寫下來 跟這個博客幾乎一模一樣了 https://blog.csdn.net/xrinosvip/article/details/80428133 我的電腦型號&#xff1a;戴爾G3 默認uefi模式&#xff0c;按f2進入的bios界面是新版跟教程上的不…

outlook日歷不顯示_如何在Outlook Online中突出顯示不同的日歷

outlook日歷不顯示If you’ve ever displayed multiple calendars in one view in Outlook Online, you’ll know how useful it is but also how confusing it can get. Use colors and charms to know at a glance which appointment belongs to which calendar. 如果您曾經在…

WinRAR 5.40 4.20 3.93 的注冊碼 - rarreg.key

把下面的數據復制到“記事本”中,用文件名“rarreg.key”命名該文件&#xff0c;保存到WinRAR安裝文件夾即完成注冊。以下4個Key隨便選一個復制都可以。WinRAR 5.40 版Key&#xff0c;復制箭頭中間內容&#xff0c;上下無空格。&#xff08;5.00版的Key 4.X和之前的3.X版本也能…

linux 下eclipse調試程序,文章2 Linux安裝Eclipse閱讀及調試程序

由于安裝Eclipse需要Java環境&#xff0c;還需要配置環境&#xff0c;非常復雜&#xff0c;建議安裝系統時&#xff0c;選擇上Eclipse開發工具但是安裝的Eclipse中沒有CDT。首先給Eclipse安裝一個CDT。1.安裝CDTEclipse菜單欄help----Install New Software.從Available Softwar…

Redis學習筆記~分布式的Pub/Sub模式

redis的客戶端有很多&#xff0c;這次用它的pub/sub發布與訂閱我選擇了StackExchange.Redis&#xff0c;發布與訂閱大家應該很清楚了&#xff0c;首先一個訂閱者&#xff0c;訂閱一個服務&#xff0c;服務執行一些處理程序&#xff08;可能是寫個日志&#xff0c;插入個數據&am…

django自定義用戶表

django自帶了用戶表。 -- auto-generated definition create table auth_user (id int auto_incrementprimary key,password varchar(128) not null,last_login datetime(6) null,is_superuser tinyint(1) not null,username varchar(150) not null,fir…

easyui關機圖標_如何在Windows 10中創建關機圖標

easyui關機圖標It’s true that shutting down your Windows 10 PC the old-fashioned way only takes three clicks. But why spend the extra energy when you can do it in two? All you have to do is create a shutdown icon, and you’ll save yourself some time. 的確…

Struts2+JFreeChart

下面以邊帖圖片和代碼的方式來講解Struts2與JFreeChart的整合。搭建環境&#xff1a;首先帖一張工程的目錄結構以及所需的jar包。注意:如果你不打算自己寫ChartResult的話只需要引入struts2-jfreechart-plugin-2.0.6.jar(這個在struts-2.0.6-all.zip可以找到了): …

STM32的FLASH ID加密

#define FLASH_ID_OFFSET 30000 //任意定義一個數 //把地址直接減去或者加上一個數是不要程序中直接出現這個地址 volatile u32 Flash_ID_addr[3]{ 0x1FFFF7E8 - FLASH_ID_OFFSET, 0x1FFFF7EC FLASH_ID_OFFSET, 0x1FFFF7F0 - FLASH_ID_OFFSET }; /**讀取STM32 FLASH ID*…

linux c視頻如何加水印,如何在Kdenlive的視頻上進行水印 | MOS86

如果你這些東西被稱為水印。他們So&#xff0c;你如何在Linux中創建水印&#xff1f;嗯&#xff0c;你這可能是Linux上最強大的開源視頻編輯器。Installation如果您尚未安裝Kdenlive&#xff0c;您應該可以在包裹管理器中找到它。在Ubuntu中&#xff0c;您還可以使用命令:sudo …

mac觸控板手勢無法使用_如何在iPad上使用觸控板手勢

mac觸控板手勢無法使用Apple蘋果Apple’s new floating Magic Keyboard case for the iPad Pro looks fantastic, but you don’t need to spend $299 to use a trackpad. Simply connect a Magic Trackpad or a third-party multi-touch trackpad to get access to all of iPa…

02.并發編程(2)Thread類源碼分析

概述 在說線程之前先說下進程&#xff0c;進程和線程都是一個時間段的描述&#xff0c;是CPU工作時間段的描述。 進程&#xff0c;是并發執行的程序在執行過程中分配和管理資源的基本單位&#xff0c;是一個動態概念&#xff0c;竟爭計算機系統資源的基本單位。每一個進程都有一…

安裝SQLserver2008

雙擊點擊setup&#xff0c;以管理員身份運行&#xff1b; 點擊安裝-》全新SQLServer獨立安裝或向現有安裝添加功能 選擇下一步&#xff0c;然后點擊具有高級服務的express版本&#xff0c;點擊下一步&#xff1b; 點擊選擇我接受許可條款&#xff0c;然后繼續點擊下一步&#x…

如何在沒有Word的情況下打開Microsoft Word文檔

Microsoft Word is part of Microsoft Office and requires an up-front purchase or a Microsoft 365 subscription. If you’re using a computer without Word installed, there are other ways to view that DOCX or DOC file. Microsoft Word是Microsoft Office的一部分&a…

redhat9Linux解壓gz,linux (redhat9)下subversion 的安裝

搞了一個subversion 花費了我兩天的時間&#xff0c;其間雖然有干其他的事情&#xff0c;但是來來回回的裝&#xff0c;搞的我是一點脾氣都沒有了&#xff0c;俗話說不經歷風雨真的見不到彩虹。就是這個意思. 原本本的下來一.準備好安裝包打算使用apache來瀏覽subversion &…

數組去重的4種方法(Which one is the fastest???嘻嘻嘻....)

<!DOCTYPE html> <html lang"en"> <head> <meta charset"UTF-8"> <title>Document</title> </head> <body> <input type"button" value"數組去重1" οnclick"show()"&g…

flask中的模型

1.什么是模型   模型&#xff0c;是根據數據庫中表的結構而創建出來的class。每一張表對應到編程語言中&#xff0c;就是一個class表中的每一個列對應到編程語言中就class中的一個屬性。 2.ORM的三大特征   1.數據表(table)到編程類(class)的映射     數據庫中的每一張…

windows復制文件路徑_如何在Windows 10上復制文件的完整路徑

windows復制文件路徑Sometimes, it’s handy to copy the full path of a file or folder in Windows 10 to the clipboard. That way, you can paste the path into an open or upload dialog quickly without having to browse for it the file. Luckily, there’s an easy w…