Problem B. Travelling Camera Problem
题目连接:
http://www.codeforces.com/gym/100253
Description
Programming competitions become very popular in Berland. Now Berland Broadcasting Corporation
(BBC) plans to organize a TV broadcast of the All-Berland Regional Contest. The contest will be in a
narrow hall stretched from the left to the right. BBC puts a long straight rail with a camera on it along
the hall. The rail starts from the left wall and goes along the hall to the right wall.
Not every position of the camera on the rail is suitable for shooting. The cameraman has chosen m
shooting positions on the rail: c1, c2, . . . , cm, where ci
is the distance (in meters) from the i-th position to
the left wall. Initially the camera will be at the position c1.
The reporter has prepared the plan showing how she will move tomorrow. She will move only along a
line parallel to the rail on the distance 1 meter from the rail. So each her position is dened by a single
number x the distance (in meters) from the left wall.
The coverage will consist of n scenes. The rst scene will be at the position x1, so the reporter will start
the live coverage there. The second scene will be at the position x2, so she will move from x1 to x2 between
the scenes. The third scene will be at the position x3 and so on. In total the reporter will successively
visit n positions x1, x2, . . . , xn, the j-th scene will be at xj .
For sure it is a bad idea to shoot the reporter if she is too far from the camera. It should be at most r
meters to the reporter at the moments of scenes.
Write a program to nd the minimum total distance the camera will move tomorrow. You may assume
that both the camera and the reporter move only along their lines, the maximum speed of the camera is
enough to keep up with the reporter.
Input
The rst line of the input contains three integer numbers m (2 ≤ m ≤ 3 · 105
), n (2 ≤ n ≤ 3 · 105
), r
(1 ≤ r ≤ 1000), where m is the number of positions where the camera can shoot, n is the number of
scenes and r is the maximum distance between the camera and the reporter in the moments of scenes.
The second line contains valid camera positions: m real numbers c1, c2, . . . , cm (0 ≤ ci ≤ 106
). The
numbers are given with exactly one digit after the decimal point. They are given in the strictly increasing
order (ci < ci+1 for each 1 ≤ i < m).
The third line contains positions of scenes in the chronological order: n real numbers x1, x2, . . . , xn
(0 ≤ xj ≤ 106
). The numbers are given with exactly one digit after the decimal point. These numbers are
not necessarily distinct.
It is guaranteed that it is possible to nd at least one valid camera position for each scene
Output
Print a single real number with at least one digit after the decimal point the minimum total distance
the camera needs to move to shoot all the scenes.
Sample Input
4 3 4
10.0 20.0 30.0 40.0
31.0 41.0 20.0
Sample Output
50.0
Hint
题意
给你摄像头的坐标,给你人依次所在的位置,你摄像头能够照到这个人的条件是,你们俩的坐标距离相差小于等于k
问你摄像头最少走多少米
题解:
对于每个人,可以划分出一个区间来,然后贪心的走最少的路程到达这个区间就好了。
代码
#include<bits/stdc++.h>
using namespace std;
int n,m;
double k;
set<double>S;
int main()
{
scanf("%d%d%lf",&n,&m,&k);
double now;
for(int i=1;i<=n;i++)
{
double x;
cin>>x;
if(i==1)now=x;
S.insert(x);
}
double ans = 0;
double len = sqrt(k*k-1);
for(int i=1;i<=m;i++)
{
double x;
cin>>x;
if(x+len>=now&&x-len<=now)continue;
double L = *S.lower_bound(x-len);
double R = *--S.upper_bound(x+len);
if(abs(L-now)<abs(R-now))
{
ans+=abs(L-now);
now=L;
}
else
{
ans+=abs(R-now);
now=R;
}
}
printf("%.12f\n",ans);
}