Codeforces Round #515 (Div. 3) C. Books Queries (模拟)

题意:有一个一维的书架,\(L\)表示在最左端放一本书,\(R\)表示在最右端放一本书,\(?\)表示从左数或从右数,最少数多少次才能得到要找的书.
题解:我们开一个稍微大一点的数组,从它的中间开始模拟,\(L\)就--\(l\)放进去,\(R\)就++\(r\)放进去,然后每次更新某一本书的最新位置,因为后放的肯定离最左侧或最右侧最近,然后两端求差弄个最小值就行.
代码:
int q;
char c;
int idx;
int ans;
int mp[2*N],now[2*N]; int main() {
ios::sync_with_stdio(false);cin.tie(0);
cin>>q;
int l=N+1,r=N;
for(int i=1;i<=q;++i){
cin>>c>>idx;
if(c=='L'){
mp[--l]=idx;
now[idx]=l;
}
else if(c=='R'){
mp[++r]=idx;
now[idx]=r;
}
else{
ans=min(now[idx]-l,r-now[idx]);
cout<<ans<<endl;
}
}
return 0;
}
Codeforces Round #515 (Div. 3) C. Books Queries (模拟)的更多相关文章
- 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. ...
- Codeforces Round #515 (Div. 3)
Codeforces Round #515 (Div. 3) #include<bits/stdc++.h> #include<iostream> #include<cs ...
- Codeforces Round #368 (Div. 2) B. Bakery (模拟)
Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...
- Codeforces Round #515 (Div. 3) 解题报告(A~E)
题目链接:http://codeforces.com/contest/1066 1066 A. Vova and Train 题意:Vova想坐火车从1点到L点,在路上v的整数倍的点上分布着灯笼,而在 ...
- Codeforces Round #284 (Div. 2)A B C 模拟 数学
A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序
A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- CodeForces Round #515 Div.3 A. Vova and Train
http://codeforces.com/contest/1066/problem/A Vova plans to go to the conference by train. Initially, ...
- Codeforces Round #515 (Div. 3) B. Heaters【 贪心 区间合并细节 】
任意门:http://codeforces.com/contest/1066/problem/B B. Heaters time limit per test 1 second memory limi ...
- Codeforces Round #515 (Div. 3) E. Binary Numbers AND Sum
E. Binary Numbers AND Sum 题目链接:https://codeforces.com/contest/1066/problem/E 题意: 给出两个用二进制表示的数,然后将第二个 ...
随机推荐
- Payment Spring Boot 1.0.4.RELEASE 发布,最易用的微信支付 V3 实现
Payment Spring Boot 是微信支付V3的Java实现,仅仅依赖Spring内置的一些类库.配置简单方便,可以让开发者快速为Spring Boot应用接入微信支付. 欢迎ISSUE,欢迎 ...
- Java Mybatis快速入门之基本使用
目录 搭建环境 编写 Mybatis 核心配置文件 pom导出资源失败 测试 搭建环境 新建Maven项目 导入Maven依赖 <dependencies> <!--mysql驱动- ...
- 【Linux】nginx详细说明
Nginx的配置文件nginx.conf配置详解如下: user nginx nginx ; Nginx用户及组:用户 组.window下不指定 worker_processes 8; 工作进程:数目 ...
- JavaScript中的事件委托机制跟深浅拷贝
今天聊下JavaScript中的事件委托跟深浅拷贝 事件委托 首先呢,介绍一下事件绑定 //方法一:通过onclick <button onclick="clickEvent()&qu ...
- RSA共模攻击
在安恒月赛中碰到一道密码学方向的ctf题 附上源码 from flag import flag from Crypto.Util.number import * p=getPrime(1024) q= ...
- 1.2V升3.3V芯片,大电流,应用MCU供电,3.3V稳压源
MCU供电一般是2.5V-5V之间等等都有,1.2V需要升到3.3V的升压芯片来稳压输出3.3V给MCU供电. 同时1.2V的输入电压低,说明供电端的能量也是属于低能量的,对于芯片自身供货是也要求高. ...
- MySQL调优之索引优化
一.索引基本知识 1.索引的优点 1.减少了服务器需要扫描的数据量 2.帮助服务器避免排序和临时表 例子: select * from emp orde by sal desc; 那么执行顺序: 所以 ...
- Java Optional使用指南
提到NullPointerException(简称NPE)异常,相信每个Java开发人员都不陌生,从接触编程的第1天起,它就和我们如影随形,最近处理的线上bug中,有不少都是对象没判空导致的NullP ...
- 【转】使用ssh-keygen和ssh-copy-id三步实现SSH无密码登录
[原]http://blog.chinaunix.net/uid-26284395-id-2949145.html ssh-keygen 产生公钥与私钥对. ssh-copy-id 将本机的公钥复制 ...
- On-the-fly Garbage Collection: an Exercise in Cooperation
On-the-fly Garbage Collection: an Exercise in Cooperation - Microsoft Research https://www.microsoft ...