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.mzph.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…