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 }
    编译人生,运行世界!
  • 相关阅读:
    C#与Java的几点区别
    用VB读取记事本的内容,控制计算器的按钮(VB6)
    通过实例说明Java中的多态
    [导入]析构函数(内存泄漏,三法则)
    [导入]const限制符
    [导入]类的一些特殊限制成员
    [导入]基类的复制控制函数
    [导入]容器适配器
    [导入]派生类到基类转换的可访问性
    [导入](复制、默认)构造函数
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4676364.html
Copyright © 2011-2022 走看看