题目描述
The New Vasjuki village is stretched along the motorway and that's why every house on it is characterized by its shift relative to some fixed point — the x_{i}xi coordinate. The village consists of nn houses, the ii -th house is located in the point with coordinates of x_{i}xi .
TELE3, a cellular communication provider planned to locate three base stations so as to provide every house in the village with cellular communication. The base station having power dd located in the point tt provides with communication all the houses on the segment [t-d,t+d][t−d,t+d] (including boundaries).
To simplify the integration (and simply not to mix anything up) all the three stations are planned to possess the equal power of dd . Which minimal value of dd is enough to provide all the houses in the village with cellular communication.
输入格式
The first line contains an integer nn ( 1<=n<=2·10^{5}1<=n<=2⋅105 ) which represents the number of houses in the village. The second line contains the coordinates of houses — the sequence x_{1},x_{2},...,x_{n}x1,x2,...,xn of integer numbers ( 1<=x_{i}<=10^{9}1<=xi<=109 ). It is possible that two or more houses are located on one point. The coordinates are given in a arbitrary order.
输出格式
Print the required minimal power dd . In the second line print three numbers — the possible coordinates of the base stations' location. Print the coordinates with 6 digits after the decimal point. The positions of the stations can be any from 00 to 2·10^{9}2⋅109 inclusively. It is accepted for the base stations to have matching coordinates. If there are many solutions, print any of them.
题意翻译
一句话题意:给定一条直线上n个点(可能重合),现在你有3个基地,你可以选择在三个地方放置这三个基地,每个基地有一个属性值d,假设将一个基地放在t的位置,那么他可以辐射到[t-d,t+d]范围内的所有的点,现在要你求出最小的d,以及这三个基地放置的位置,保留6位小数
输入输出样例
输入 #1复制
4 1 2 3 4输出 #1复制
0.500000 1.500000 2.500000 3.500000输入 #2复制
3 10 20 30输出 #2复制
0 10.000000 20.000000 30.000000输入 #3复制
5 10003 10004 10001 10002 1输出 #3复制
0.500000 1.000000 10001.500000 10003.500000
思路:
二分答案+二分查找
二分答案用来返回最小的d
二分查找用来查找某个基地第一个不能覆盖的下标
首先确定 给定一个d,如何尽最大可能地覆盖到所有的点?
第一个基地建在 x{0} + d 的地方,然后可以知道第二个基地的左边界是第一个基地不能覆盖到的地方x{index_01},第三个基地的左边界是第二个基地不能覆盖到的地方x{index_01}
然后第三个基地的右边界就是这个d最大能覆盖的距离了.
二分答案的 check函数 如何去写?
如果照以上思路得到的最大距离能覆盖到max(x{1}...max{n})那么就返回true 不能就返回 false
如果以上能够理解的话:
先要排序
这里可以发现题目给定的x{i}都是整数,那么可以得到结论2*d一定是整数.可以自己想想
如果直接去找d的会出现精度问题...
我卡了一个晚上,这边的建议是直接去直径2*d,然后d的结果小数一定为.000000 或者.500000
给出c++代码:
#include <bits/stdc++.h>
#define MAXN 200000+10
#define LL long long
using namespace std;
int n;
int a[MAXN];
//二分查找第一个大于target的值
int find_not_covered(double ai,int mid){
int target = ai + mid;
if(a[n-1] <= target){
return -1;
}
int l = 0, r = n-1;
while(l<=r){
int mid = (l+r)/2;
if(a[mid]<=target){
l = mid+1;
}else{
r = mid-1;
}
}
//r下标是最后一个满足a[i]<=target的,r+1就是第一个满足a[i]>target
return r+1;
}
//检查这个直径是否能将所有的点覆盖 能返回true 不能返回 false
bool check(int mid){
int index = find_not_covered(a[0],mid);
if(index == -1) return true;
index = find_not_covered(a[index],mid);
if(index == -1) return true;
//能覆盖到的最大值 与 最大值作比较
return (a[index] + mid) >= a[n-1];
}
//打印
void printans(int mid){
double flag1 = a[0] + mid*0.5;
int index = find_not_covered(a[0],mid);
// cout << "第一个基地未能覆盖到的:" << a[index] <<endl;
double flag2 = a[index] + mid*0.5;
index = find_not_covered(a[index],mid);
// cout << "第二个基地未能覆盖到的:" << a[index] <<endl;
double flag3 = a[index] + mid*0.5;
printf("%.6lf %.6lf %.6lf",flag1,flag2,flag3);
}
int main(){
cin >> n;
for(int i = 0 ; i < n ; i++){
cin >> a[i];
}
sort(a,a+n);
int l = 0,r = (a[n-1]-a[0]);
while(l<=r){
//直径只能是整数,因为基站能覆盖的范围的端点一定是a[i],而a[i]是题目给定,是整数
int mid = l + (r-l)/2;
// cout << "l:" << l << ",mid:"<<mid << ",r:" << r <<endl;
if(check(mid)){
r = mid-1;
}else{
l = mid+1;
}
}
printf("%.6lf\n",l*0.5);
printans(l);
return 0;
}