Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4932
Miaomiao's geometrytime limit: 2000/1000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 410 accepted submission (s): 147
Problem descriptionthere are n point on X-axis. miaomiao wowould like to cover them all by using segments with same length.
There are 2 limits:
1. A point is convered if there is a segments t, the point is the left end or the right end of T.
2. The length of the intersection of any two segments equals zero.
For example, point 2 is convered by [2, 4] and not convered by [1, 3]. [1, 2] and [2, 3] are legal segments, [1, 2] and [3, 4] are legal segments, but [1, 3] and [2, 4] are not (the length of intersection doesn't equals zero), [1, 3] and [3, 4] are not (not the same length ).
Miaomiao wants to maximum the length of segements, please tell her the maximum length of segments.
For your information, the point can't coincidently at the same position. inputthere are several test cases.
There is a number T (t <= 50) on the first line which shows the number of test cases.
For each test cases, there is a number N (3 <= n <= 50) on the first line.
On the second line, there are n integers AI (-1e9 <= AI <= 1e9) shows the position of each point. outputfor each test cases, output a real number shows the answser. please output three digit after the decimal point. sample Input
331 2 331 2 441 9 100 10
Sample output
1.0002.0008.000HintFor the first sample , a legal answer is [1,2] [2,3] so the length is 1.For the second sample , a legal answer is [-1,1] [2,4] so the answer is 2.For the thired sample , a legal answer is [-7,1] , [1,9] , [10,18] , [100,108] so the answer is 8.
Sourcebestcoder round #4
Question:
The maximum value can cover the Interval Length of all given points (the given point must be at both ends of the interval ).
Ideas:
The answer must be the difference between adjacent points or the difference between adjacent points divided by 2. Then, calculate these possible answers and start enumeration from the largest one for verification.
The Code is as follows:
# Include <cstdio> # include <algorithm> # include <cstring> using namespace STD; const int maxn = 147; int f [maxn]; // record the line segment direction Double P [maxn]; double d [maxn]; // The difference int n between adjacent breakpoints; void Init () {memset (p, 0, sizeof (p); memset (F, 0, sizeof (f); memset (D, 0, sizeof (d);} bool judge (double TT) {int I; for (I = 1; I <n-1; I ++) {If (P [I]-TT <p [I-1] & P [I] + tt> P [I + 1]) break; // both left and right do not match if (P [I]-tt> = P [I-1]) // view left {If (F [I-1] = 2) // if the previous one is right {If (P [I]-P [I-1] = TT) f [I] = 1; // two vertices act as two endpoints of a line segment else if (P [I]-P [I-1]> = 2 * TT) // One to the left {f [I] = 1;} else if (P [I] + TT <= P [I + 1]) {f [I] = 2; // only right} else return false;} else f [I] = 1 ;} else if (P [I] + TT <= P [I + 1]) f [I] = 2;} if (I = N-1) // all return true; return false;} int main () {int t; scanf ("% d", & T); While (t --) {Init (); scanf ("% d ", & N); For (INT I = 0; I <n; I ++) {scanf ("% lf", & P [I]);} Sort (p, P + n); int cont = 0; For (INT I = 1; I <n; I ++) {d [cont ++] = P [I]-P [I-1]; d [cont ++] = (P [I]-P [I-1])/2.0 ;} sort (D, D + cont); double ans = 0; For (INT I = cont-1; I> = 0; I --) {memset (F, 0, sizeof (f); F [0] = 1; // the starting line must be directed to the left if (Judge (d [I]) {ans = d [I]; break;} printf ("%. 3lf \ n ", ANS);} return 0 ;}