CodeForces - 344E Read Time (模拟题 + 二分法)
1 second
256 megabytes
standard input
standard output
Mad scientist Mike does not use slow hard disks. His modification of a hard drive has not one, but n different heads that can read
data in parallel.
When viewed from the side, Mike's hard drive is an endless array of tracks. The tracks of the array are numbered from left to right with integers, starting with 1. In the initial state the i-th
reading head is above the track number hi.
For each of the reading heads, the hard drive's firmware can move the head exactly one track to the right or to the left, or leave it on the current track. During the operation each head's movement does not affect the movement of the other heads: the heads
can change their relative order; there can be multiple reading heads above any of the tracks. A track is considered read if at least one head has visited this track. In particular, all of the
tracks numbered h1, h2, ..., hn have
been read at the beginning of the operation.
Mike needs to read the data on m distinct tracks with numbers p1, p2, ..., pm.
Determine the minimum time the hard drive firmware needs to move the heads and read all the given tracks. Note that an arbitrary number of other tracks can also be read.
The first line of the input contains two space-separated integers n, m (1 ≤ n, m ≤ 105)
— the number of disk heads and the number of tracks to read, accordingly. The second line contains n distinct integers hi in
ascending order (1 ≤ hi ≤ 1010, hi < hi + 1)
— the initial positions of the heads. The third line contains m distinct integers pi in
ascending order (1 ≤ pi ≤ 1010, pi < pi + 1)
- the numbers of tracks to read.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is recommended to use the cin, cout streams
or the%I64d specifier.
Print a single number — the minimum time required, in seconds, to read all the needed tracks.
3 4
2 5 6
1 3 6 8
2
3 3
1 2 3
1 2 3
0
1 2
165
142 200
81
The first test coincides with the figure. In this case the given tracks can be read in 2 seconds in the following way:
- during the first second move the 1-st head to the left and let it stay there;
- move the second head to the left twice;
- move the third head to the right twice (note that the 6-th track has already been read at the beginning).
One cannot read the tracks in 1 second as the 3-rd head is at distance 2 from the 8-th track.
通过二分枚举时间,知道满足条件
#include <cstdio> |
CodeForces - 344E Read Time (模拟题 + 二分法)的更多相关文章
- Codeforces 767B. The Queue 模拟题
B. The Queue time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...
- Codeforces 691C. Exponential notation 模拟题
C. Exponential notation time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...
- CodeForces - 344B Simple Molecules (模拟题)
CodeForces - 344B id=46665" style="color:blue; text-decoration:none">Simple Molecu ...
- CodeForces - 344D Alternating Current (模拟题)
id=46667" style="color:blue; text-decoration:none">CodeForces - 344D id=46667" ...
- CodeForces 681C Heap Operations (模拟题,优先队列)
题意:给定 n 个按顺序的命令,但是可能有的命令不全,让你补全所有的命令,并且要求让总数最少. 析:没什么好说的,直接用优先队列模拟就行,insert,直接放入就行了,removeMin,就得判断一下 ...
- CodeForces - 427B (模拟题)
Prison Transfer Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Sub ...
- Educational Codeforces Round 2 A. Extract Numbers 模拟题
A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...
- Codeforces Beta Round #7 B. Memory Manager 模拟题
B. Memory Manager 题目连接: http://www.codeforces.com/contest/7/problem/B Description There is little ti ...
- Codeforces Beta Round #5 B. Center Alignment 模拟题
B. Center Alignment 题目连接: http://www.codeforces.com/contest/5/problem/B Description Almost every tex ...
随机推荐
- PageHelper在Mybatis中的使用
环境:Spring 4.2.1 Mybatis 3.2.8 pagehelper 5.1.2 Mybatis官方教程:https://github.com/pagehelper/Mybatis-Pag ...
- Python 获取计算机全名(fully qualified host name)
Python 获取计算机全名(fully qualified host name) import socket socket.getfqdn() socket.gethostname()
- oracle中
select tmp_tb.*, ROWNUM row_id from (SELECT MX.* --这里不能直接用* ...
- POJ 3122 Pie【二分答案】
<题目链接> 题目大意: 将n个半径不一但是高度为1的蛋糕分给 F+1个人,每个人分得蛋糕的体积应当相同,并且需要注意的是,每个人分得的整块蛋糕都只能从一个蛋糕上切下来,而不是从几个蛋糕上 ...
- JavaScript 六大类运算符(详细~)
运算符 1.算术运算符:+.-.*./.%.++.-- 单目(操作数为一个且为变量): i++.i--.++i.--i; 双目:+.-.*./.%; 双目其中 -*/% ...
- Spring Boot 静态资源访问原理解析
一.前言 springboot配置静态资源方式是多种多样,接下来我会介绍其中几种方式,并解析一下其中的原理. 二.使用properties属性进行配置 应该说 spring.mvc.static-pa ...
- XamarinEssentials教程首选项Preferences判断项目是否存在
XamarinEssentials教程首选项Preferences判断项目是否存在 首选项也可以称为偏好设置.它以键值对的形式保存数据,适用于用户少量数据的存储.例如,将用户的个性化设置保存后,每次打 ...
- Math.round()
在 JAVA 中四舍五入采用 Math.round(T a) 函数,函数返回的是一个 long 类型的长整型,参数 a 可以是 double 也可以是 float. 查看 JDK 源码: public ...
- ES6字符串和正则表达式改动
1. ES6字符串变更 (1)includes() 方法,如果在字符串中检测到指定文本返回true,否则返回false (2)startsWith()方法,如果在字符串的起始部分检测到文本,则返回tr ...
- 安装并运行Hello World
新建虚拟环境并安装Flask pip install Flask 运行HelloWorld from flask import Flask #导入Flask类 app = Flask(__name__ ...