HDU 4932 Miaomiao & amp; #39; s Geometry (inference)

Source: Internet
Author: User

HDU 4932 Miaomiao & #39; s Geometry (inference)
HDU 4932 Miaomiao's Geometry

Question Link

Given some vertices on the X axis (not repeated), you need to select a line segment so that it can be placed in these intervals, ensure that the line segment does not cross the point (that is, the line segment can only be the leftmost or rightmost is the point), and there is no line segment intersection, find the maximum line segment that can be placed in

Train of Thought: reasoning, there are only two lines between two points, and half of the line segment may match the meaning of the question. Then, for each line segment, determine whether it can be successfully put in. In this step, we use greed to give priority to the left, not on the right.

Code:

#include 
 
  #include 
  
   #include 
   
    #include using namespace std;const int N = 55;const double eps = 1e-9;int t, n;double a[N];bool notless(double a, double b) {    if (fabs(a - b) < eps) return true;    return a > b;}bool judge(double len) {    int flag = 1;    for (int i = 2; i < n; i++) {if (flag && notless(a[i] - a[i - 1], len))    continue;else if (flag && a[i] - a[i - 1] < len && notless(a[i + 1] - a[i], len * 2))    continue;else if (flag && a[i] - a[i - 1] < len && notless(a[i + 1] - a[i], len)) {    if (fabs(a[i + 1] - a[i] - len) >= eps)flag = 0;    continue;}else if (!flag && notless(a[i + 1] - a[i], len * 2)) {    flag = 1;    continue;}else if (!flag && notless(a[i + 1] - a[i], len)) {    if (fabs(a[i + 1] - a[i] - len) < eps)flag = 1;    continue;}return false;    }    return true;}int main() {    scanf("%d", &t);    while (t--) {scanf("%d", &n);for (int i = 1; i <= n; i++)    scanf("%lf", &a[i]);sort(a + 1, a + 1 + n);double ans = 0;for (int i = 1; i < n; i++) {    double len = a[i + 1] - a[i];    if (judge(len))ans = max(ans, len);    len /= 2;    if (judge(len))ans = max(ans, len);}printf("%.3lf\n", ans);    }    return 0;}
   
  
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.