https://codeforces.com/problemset/problem/51/C
题目
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 xi coordinate. The village consists of n houses, the i-th house is located in the point with coordinates of 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 d located in the point t provides with communication all the houses on the segment [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 d. Which minimal value of d is enough to provide all the houses in the village with cellular communication.
Input
The first line contains an integer n (1 ≤ n ≤ 2·105) which represents the number of houses in the village. The second line contains the coordinates of houses — the sequence x1, x2, ..., xn of integer numbers (1 ≤ xi ≤ 109). It is possible that two or more houses are located on one point. The coordinates are given in a arbitrary order.
Output
Print the required minimal power d. 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 0 to 2·109 inclusively. It is accepted for the base stations to have matching coordinates. If there are many solutions, print any of them.
题解
二分+贪心
二分功率,贪心验证能否成立……
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<set> #include<iomanip> #define REP(r,x,y) for(register int r=(x); r<(y); r++) #define REPE(r,x,y) for(register int r=(x); r<=(y); r++) #ifdef sahdsg #define DBG(...) printf(__VA_ARGS__) #else #define DBG(...) (void)0 #endif using namespace std; typedef long long LL; typedef pair<LL, LL> pll; typedef pair<int, int> pii; #define MAXN 200007 #define EPS 1e-3 int n; int arr[MAXN]; char ch; int f; inline void read(int &x) { x=0; f=1; do ch=getchar(); while(!isdigit(ch) && ch!='-'); if(ch=='-') ch=getchar(),f=-1; while(isdigit(ch)) {x=x*10+ch-'0'; ch=getchar();} x*=f; } inline int ub(int f, int t, double x) { while(f<t) { int m=(f+t)>>1; if(arr[m]<=x) f=m+1; else t=m; } return f; } inline int lb(int f, int t, double x) { while(f<t) { int m=(f+t)>>1; if(arr[m]<x) f=m+1; else t=m; } return f; } inline bool vali(double m) { int s=arr[0], x=0; REP(i,0,3) { x=ub(x, n, s+m); if(x>=n) {return 1;} s=arr[x]; } DBG("#%d ", x); return false; } int main() { read(n); REP(i,0,n) { read(arr[i]); } sort(arr,arr+n); double l=0, r=1e9; while(r-l>EPS) { double m = (l+r)/2; if(vali(m)) { r=m; } else { l=m; } } printf("%.2f ", r/2); int s=arr[0], x=0; REP(i,0,3) { if(i) putchar(' '); int lx=ub(x, n, s+r)-1; printf("%f", (arr[lx]+arr[x])/2.0); x=lx+1; if(x>=n) { for(i++;i<3;i++) { if(i) putchar(' '); printf("0"); } return 0; } s=arr[x]; } return 0; }