Codeforces Round #200 (Div. 2)344E Read Time(二分)

限时加码!20+主流AI编程工具免费用 购周边加赠Coding Plan Lite,Claude Code、Cursor等即刻畅享,学习进阶更高效! 阅读详情

E. Read Time
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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 h1h2...hn have been read at the beginning of the operation.

Mike needs to read the data on m distinct tracks with numbers p1p2...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.

Input

The first line of the input contains two space-separated integers nm (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 ≤ 1010hi < hi + 1) — the initial positions of the heads. The third line contains m distinct integers pi in ascending order (1 ≤ pi ≤ 1010pi < 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 cincout streams or the%I64d specifier.

Output

Print a single number — the minimum time required, in seconds, to read all the needed tracks.

Sample test(s)
input
3 4
2 5 6
1 3 6 8
output
2
input
3 3
1 2 3
1 2 3
output
0
input
1 2
165
142 200
output
81
Note

The first test coincides with the figure. In this case the given tracks can be read in 2 seconds in the following way:

  1. during the first second move the 1-st head to the left and let it stay there;
  2. move the second head to the left twice;
  3. 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.



有n个磁头,m个位置需要访问,磁头每一秒移动一格,求访问m个磁头需要的最少时间。二分寻找答案。


AC代码:


#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
const int MAXN = 1e5 + 5;
typedef long long ll;
int n, m;
ll a[MAXN], b[MAXN];
bool judge(ll x)
{
	int cur = 1;
	ll y;
	for(int i = 1; i <= n; ++i) {
		if(abs(a[i] - b[cur]) > x) continue;
		if(a[i] == b[cur]) cur++;
		if(a[i] > b[cur]) y = max(a[i] + x - 2 * (a[i] - b[cur]), a[i] + (x - (a[i] - b[cur])) / 2);
		else y = a[i] + x;
		while(b[cur] <= y && cur <= m) cur++;
	}
	return cur > m;
}
int main(int argc, char const *argv[])
{
	cin >> n >> m;
	for(int i = 1; i <= n; ++i)
		cin >> a[i];
	for(int i = 1; i <= m; ++i)
		cin >> b[i];
	ll l = -1, r = abs(a[1] - b[1]) * 2 + abs(a[1] - b[m]), mid;
	while(l + 1 < r) {
		mid = (l + r) >> 1;
		if(judge(mid)) r = mid;
		else l = mid;
	}
	cout << r << endl;
	return 0;
}


LLM API调用中的并发与并行:性能、成本与稳定性的工程抉择 并发与并行是分布式系统中优化I/O和计算资源的核心执行模型。并发通过事件循环实现高密度任务调度,擅长掩盖网络等待;并行依托多进程突破GIL限制,专精CPU密集型处理。在LLM API调用场景中,二者并非替代关系,而是针对网络层(高并发有效)、序列化层(高并行有效)和胶水层(需混合编排)的瓶颈特性进行正交优化。真实压测表明,纯asyncio易触发RateLimit错误,而混合执行模式可降低P95延迟62%、减少429错误率超97%,显著提升吞吐与成本效率。本文基于GPT-4-turbo等主流模型的生产实践,解 阅读详情

相关推荐

OpenCV人脸识别实战:从Haar Cascade检测到LBPH识别全流程解析

人脸识别是计算机视觉领域的经典应用,其核心原理是通过算法提取人脸特征并进行比对。传统方法如Haar Cascade和LBPH(局部二值模式直方图)因其实现简单、计算高效,成为理解底层机制和快速原型开发的理想选择。Haar Cascade利用矩形特征和级联分类器实现快速人脸检测,而LBPH则通过局部纹理描述子构建特征直方图进行识别,两者结合在资源受限场景下具有重要技术价值。在工程实践中,这类方案适用于门禁考勤、桌面应用等小规模封闭集合的识别场景。本文以OpenCV为工具,详细拆解了从环境搭建、数据采集到检测、

weixin_30480075的博客 374

CodeForces - 344E Read Time (模拟题 + 二分)

CodeForces - 344E Read Time Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Mad scientist Mike does not use slow

qq_18661257的专栏 1556

ESXI 8.0.3 ubuntu 24.04 直通显卡P40

手把手,一步步教你直通GPU,ESXI 8.0.3 ubuntu 24.04 直通显卡P40

qq_36433118的博客 4650

Codeforces 344E - Read Time

二分后贪心#include<bits/stdc++.h> using namespace std; #define LL long long const int maxn = 112345; const LL INFF = 0x3f3f3f3f3f3f3f3fll; LL h[maxn],p[maxn]; int n,m;bool check(LL x){ int pos = 0;

lambda QAQ 350

Codeforces 344E Read Time [二分答案]【思维】

题目链接:http://codeforces.com/contest/344/problem/E——————————————–. E. Read Time time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Mad scientis

Tabris的博客 765

codeforces 992E. Nastya and King-Shamans

树状数组 二分 题目传送门 题目大意: 维护一个数列,每次操作为先修改一个数,再询问是否存在一个位置iii满足w[i]=sum[i−1]w[i]=sum[i-1]w[i]=sum[i−1]并输出这个位置。 妙蛙 问题要求出满足 Ax=sumx−1A_x=sum_{x−1}Ax​=sumx−1​ 的位置,这个可以转化为 sumx=2sumx−1sum_x=2sum_{x−1}sumx​=2sumx...

forezxl的博客 480

Codeforces 344

A: 扫一遍。 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 5 using namespace std; 6 7 int n; 8 9 char s1[3],s2[3]; 10 11 int main() 12 { 13 ...

weixin_30651273的博客 120

Codeforces Round #200 (Div. 1) C. Read Time

Codeforces Round #200 (Div. 1) C. Read Time 题目链接 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

旺崽的博客 2667

Codeforces Round #200 (Div. 1)

A Rational Resistance思路:因为每个电阻的电阻是1,这就很好算了,比如要求的电阻是a/b,假设a>b,则要求电阻个数就是a/b + b/(a%b) + (a%b)/(b%(a%b))…,直到某一步的模运算那里能够整除。我也不知道为啥这样,找了俩数据,算了算是这个规律,那就这样算了。#include <bits/stdc++.h> using namespace std; type

梦幻的蔷薇色 616

Codeforces Round #200 (Div. 1) C. Read Time 二分

/** Codeforces Round #200 (Div. 1) C. Read Time 二分 链接:https://codeforces.com/contest/343/problem/C 题意:n个刷子 m个原点 n个刷子同时左右进行刷 问最少的时间使得m个点都能够被刷到; 分析 : 直接二分暴力查找答案 由于刷子存在的点和原点都是递增的 因此每次看枚举的次数看是否能够遍历所有的点...

布呗之路 315

Codeforces Round #200 (Div. 2) 题解

A. Magnets   题意:给你一列按照顺序的磁铁0代表负极,1代表正极,询问你这些磁铁是几组极性相反的磁铁可以连接在一起变成一组   思路:按照字符串把这些磁铁都读入,然后用一个ans变量记录最后的答案,最少有一组,如果当前的一小块磁铁与上一块不同,ans++,因为他们肯定不能连接在一起,同极排斥,相同就接在一起   代码: #include <bits/stdc++....

weixin_30762087的博客 319

Codeforces Round #200 (Div. 2)E

Read Time 题意:有一个数组,很多指针指在这个数组上,每次每个指针可以向左或向右移动一个位置。给出一些需要访问的位置,问访问用的最少时间。 一个指针只可能转一次方向。二分答案。 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define MAX(...

weixin_34289454的博客 159

Codeforces Round #275 (Div. 1)C(状压+期望)

C. Game with Strings time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You play the game with your friend. The

cq_phqg 1014

Colab中用PySpark高效读取Kaggle大文件的实战指南

PySpark是一种面向大规模数据处理的分布式计算框架,其核心优势在于内存外(out-of-core)执行、惰性求值和JVM级内存管理,特别适合突破单机内存瓶颈。在资源受限但开箱即用的Google Colab环境中,PySpark可替代pandas处理远超RAM容量的Kaggle数据集(如5–10GB CSV/Parquet),通过合理配置spark.sql.files.maxPartitionBytes与spark.sql.adaptive.enabled等关键参数,实现稳定、可复现、低内存占用的数据加载

445

CodeForces - 344E Read Time && CodeForces - 830A Office Keys 二分+贪心

题目链接   cf 344e   题意: 有n个磁头,m个位置需要访问,磁头每一秒移动一格,求访问m个磁头需要的最少时间。 思路:   首先老规矩,求最少最多的问题,立马想到二分,对于这个题目,很明显二分时间带入验证可行. 那么问题就转化成了怎么验证?   要想在一定时间内访问多的磁道,那么基于贪心的思想,如果有磁道在当前磁头的左边

Marcus-Bao的个人主页 1422

Read Time CodeForces - 343C (二分查找答案)

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 e...

dsaghjkye的博客 474

codeforces 992

A. Nastya and an Array水题,暴力#include&lt;iostream&gt; #include&lt;algorithm&gt; #include&lt;cstdio&gt; #include&lt;cstring&gt; #include&lt;string&gt; #include&lt;cmath&gt; #include&lt;cstdlib&gt; #inclu

常胜将军的博客 502

Codeforces Round #321 (Div. 2) 580C Kefa and Park(dfs)

题目链接: 第一行给你n、m,接下来的一行有n个数,如果为1则代表这个点有cat,接下来的n-1行每行给出x、y,表示x、y相连。问你从1出发 能到达的叶子结点有几个,要求到达叶子结点的路上连续有cat的点不超过m。 a数组保存结点是否含有cat,从1这个点开始dfs,每次dfs对cat数组操作,记录连续的含有cat的点数,回溯时再判断当前结点是否满足 条件,要求结点为叶子

GKHack's Blog 2034

Codeforces Round #325 (Div. 2) 586ABC题解

题目链接:点击打开链接 A: 给出Alena的课表, 1代表有课, 0代表没课, 2个连续0以上可以回家休息, 问你一天在学校待多久. 直接模拟即可, 可以直接计算1, 101的数目. 我记录了开头的1和结尾的1, 然后减去2个连续0以上的个数就是答案. AC代码: #include "iostream" #include "cstdio" #include "cstring" #

GKHack's Blog 1707
上一篇: Codeforces Round #200 (Div. 2)344D Alternating Current(栈)
下一篇: HDOJ2041 超级楼梯(dp & 打表)
GKHack
博客等级 码龄11年 48粉丝 291原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值