zoukankan      html  css  js  c++  java
  • 枚举+贪心 HDOJ 4932 Miaomiao's Geometry

    题目传送门

     1 /*
     2     题意:有n个点,用相同的线段去覆盖,当点在线段的端点才行,还有线段之间不相交
     3     枚举+贪心:有坑点是两个点在同时一条线段的两个端点上,枚举两点之间的距离或者距离一半,尽量往左边放,否则往右边放,
     4                 判断一下,取最大值。这题二分的内容少
     5 */
     6 #include <cstdio>
     7 #include <algorithm>
     8 #include <cstring>
     9 #include <cmath>
    10 using namespace std;
    11 
    12 const int MAXN = 55;
    13 const int INF = 0x3f3f3f3f;
    14 double x[MAXN];
    15 int n;
    16 
    17 bool check(double len)   {
    18     int last = -1;
    19     for (int i=1; i<=n; ++i)    {
    20         if (i == 1 || i == n) continue;
    21         if (last == -1) {
    22             if (x[i] - len >= x[i-1]) {
    23                 last = -1;  continue;
    24             }
    25             else if (x[i] + len <= x[i+1])  {
    26                 last = 1;   continue;
    27             }
    28             else    return false;
    29         }
    30         else if (last == 1) {
    31             if (x[i-1] + len == x[i])   {
    32                 last = -1;   continue;
    33             }
    34             else if (x[i] - len >= x[i-1] + len) {
    35                 last = -1;  continue;
    36             }
    37             else if (x[i] + len <= x[i+1])  {
    38                 last = 1;   continue;
    39             }
    40             else    return false;
    41         }
    42     }
    43     return true;
    44 }
    45 
    46 int main(void)  {       //HDOJ 4932 Miaomiao's Geometry
    47     //freopen ("HDOJ_4932.in", "r", stdin);
    48 
    49     int T;  scanf ("%d", &T);
    50     while (T--) {
    51         scanf ("%d", &n);
    52         for (int i=1; i<=n; ++i)    {
    53             scanf ("%lf", &x[i]);
    54         }
    55         sort (x+1, x+1+n);
    56 
    57         double ans = 0.0;
    58         for (int i=2; i<=n; ++i)    {
    59             if (check (x[i] - x[i-1]))  ans = max (ans, x[i] - x[i-1]);
    60             if (check ((x[i] - x[i-1]) * 0.5))  ans = max (ans, (x[i] - x[i-1]) * 0.5);
    61         }
    62 
    63         printf ("%.3f
    ", ans);
    64     }
    65 
    66     return 0;
    67 }
    编译人生,运行世界!
  • 相关阅读:
    SQL查询
    SQL总结(二)连表查询
    SQL总结(一)基本查询
    SQL游标、函数的使用方法
    理解 Virtual DOM(摘)及评价
    virtual DOM的作用:将DOM的维护工作由系统维护转交给virtual DOM维护
    DOM的回流和重绘(重排、重绘)
    并发编程的核心
    执行流(指令流)的控制层级
    go语言的工资好高
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4676364.html
Copyright © 2011-2022 走看看