码迷,mamicode.com
首页 > 其他好文 > 详细

枚举+贪心 HDOJ 4932 Miaomiao's Geometry

时间:2015-07-25 19:41:36      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:

 

题目传送门

 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\n", ans);
64     }
65 
66     return 0;
67 }

 

枚举+贪心 HDOJ 4932 Miaomiao's Geometry

标签:

原文地址:http://www.cnblogs.com/Running-Time/p/4676364.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!