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

时间:2024-04-16 12:07:04

C. Read Time

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/343/problem/C

Description

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.

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

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.

​i​​(A​i​​≤10​6​​) 分,之后每过一分钟这道题目的分值会减少 BiB_iB​i​​ 分,并且保证到比赛结束时分值不会减少为负值。比如,一个人在第 xxx 分钟结束时做出了第 iii 道题目,那么他/她可以得到 Ai−Bi∗xA_i - B_i * xA​i​​−B​i​​∗x 分。
若一名选手在第 xxx 分钟结束时做完了一道题目,则他/她可以在第 x+1x+1x+1 分钟开始时立即开始做另一道题目。
参加省队选拔的选手 dxy 具有绝佳的实力,他可以准确预测自己做每道题目所要花费的时间,做第 iii 道需要花费 Ci(Ci≤t)C_i(C_i \leq t)C​i​​(C​i​​≤t) 分钟。由于 dxy 非常神,他会做所有的题目。但是由于比赛时间有限,他可能无法做完所有的题目。他希望安排一个做题的顺序,在比赛结束之前得到尽量多的分数

Input

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.,B​i​​,C​i​​,即此题的初始分值、每分钟减少的分值、dxy做这道题需要花费的时间。

Output

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

Sample Input

3 4
2 5 6
1 3 6 8

Sample Output

2

HINT

题意

有n个刷子,每个刷子都可以左移动和右移动,每移动一格是1秒

然后问你最少多少秒,可以让这n个刷子把这m个接口都刷完

题解:

二分时间之后check就好

check注意可以先往左再往右,也可以先往右再往左的

注意一下就好啦

代码:

#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std; long long a[];
long long b[];
int n,m;
long long abs(long long a)
{
if(a<)return -a;
return a;
}
int judge(long long t)
{
int tot = ;
int l=,r=;
for(int i=;i<=n;i++)
{
long long ans = abs(b[r]-b[l]) + min(abs(b[r]-a[i]),abs(b[l]-a[i]));
while(r<=m&&ans<=t)
{
r++;
ans = abs(b[r]-b[l]) + min(abs(b[r]-a[i]),abs(b[l]-a[i]));
}
l=r;
if(r==m+)return ;
}
return ;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
for(int i=;i<=m;i++)
scanf("%lld",&b[i]);
long long l = ,r = 2e10;
while(l<=r)
{
long long mid = (l+r)/2LL;
if(judge(mid))r = mid - ;
else l = mid + ;
}
cout<<l<<endl;
}