Hdu 4932 Miaomiao & #39; s Geometry (violent)
Link: hdu 4932 Miaomiao's Geometry
There are several points on the x coordinate. Now we need to overwrite these points with several lines of equal length. If a point is covered by a line segment, it must be at the left or right endpoint of the line, and the positions of each line segment cannot overlap, so the maximum length is obtained.
Solution: There are pitfalls in this question. During the competition, the o (n) algorithm looks for the shortest distance between two points. However, this does not work at the beginning, for example,-1 0 10 12 18 20. In this case, the minimum maintenance time should be 12 ~ 18. The length is 6. This line segment can cover 12 and 18 points, and then-1 and 20 are at both ends. Therefore, there are only two points: 0 and 10. The length between 0 and 10 is 10 and greater than 6. Therefore, the two ends overlap.
After the game, enumerate the length between two points and half of the length to determine whether it is feasible and maintain the maximum value.
#include
#include
#include
#include using namespace std;const int maxn = 100;int n;double arr[maxn];bool judge (double w) { double tmp = arr[0]; for (int i = 1; i < n; i++) { if (fabs(arr[i] - tmp) < 1e-9 || arr[i] - tmp > w || fabs(arr[i] - tmp - w) < 1e-9) tmp = arr[i]; else if (arr[i] < tmp) return false; else tmp = arr[i] + w; } return true;}int main () { int cas; scanf("%d", &cas); while (cas--) { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lf", &arr[i]); sort(arr, arr + n); double ans = 0; for (int i = 1; i < n; i++) { if (judge (arr[i] - arr[i-1])) ans = max(ans, arr[i] - arr[i-1]); else if (judge((arr[i] - arr[i-1]) / 2)) ans = max(ans, (arr[i] - arr[i-1]) / 2); } printf("%.3lf\n", (double)ans); } return 0;}