贪心/poj 1328 Radar Installation

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 using namespace std;
 6 struct node
 7 {
 8     double right,left;
 9 };
10 node a[1010];
11 int ans,n,d;
12 bool flag;
13
14 bool cmp(node x,node y)
15 {
16     return x.left<y.left;
17 }
18
19 int main()
20 {
21     int T=0;
22     scanf("%d%d",&n,&d);
23     while (n!=0)
24     {
25         T++;
26         flag=true;
27         ans=0;
28         memset(a,0,sizeof(a));
29         for (int i=1;i<=n;i++)
30         {
31             int xx,yy;
32             scanf("%d%d",&xx,&yy);
33             if (yy>d)
34             {
35                 flag=false;
36                 continue;
37             }
38             double dx=sqrt(d*d-yy*yy);
39             a[i].left=xx-dx;
40             a[i].right=xx+dx;
41         }
42         if (flag==false) ans=-1;
43         else
44         {
45             sort(a+1,a+n+1,cmp);
46             double last=a[1].right;
47             ans=1;
48             for (int i=2;i<=n;i++)
49             {
50                 if (a[i].left>last)
51                 {
52                     ans++;
53                     last=a[i].right;
54                 }
55                 else if (a[i].right<last) last=a[i].right;
56             }
57         }
58         printf("Case %d: %d\n",T,ans);
59         scanf("%d%d",&n,&d);
60     }
61     return 0;
62 }
时间: 2024-10-31 18:30:13

贪心/poj 1328 Radar Installation的相关文章

POJ 1328 Radar Installation 贪心题解

本题是贪心法题解,不过需要自己观察出规律,这就不容易了,很容易出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 按照x轴大小排序 2 从最左边的点循环,首先找到最小x轴的圆 3 以这个圆判断可以包括右边的多少个圆,直到不可以包括下一个点,那么继续第2步,画一个新圆. 看代码吧,应该很清晰直观的了. 效率是O(n),虽然有嵌套循环,但是下标没有重复,一遍循环就可以了,故此是O(n). #include <stdio.h> #include <cmath> #incl

Poj 1328 Radar Installation 贪心

题目链接:http://poj.org/problem?id=1328 Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52768   Accepted: 11867 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the othe

[ACM] POJ 1328 Radar Installation (贪心,区间选点问题)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 51131   Accepted: 11481 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca

POJ 1328 Radar Installation 雷达安装 贪心问题求解

题目链接: POJ 1328 Radar Installation Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coas

[2016-02-04][POJ][1328][Radar Installation]

[2016-02-04][POJ][1328][Radar Installation] Radar Installation Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Submit Status Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in

poj 1328 Radar Installation (贪心)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52823   Accepted: 11883 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca

POJ 1328 Radar Installation 【贪心 区间选点】

解题思路:给出n个岛屿,n个岛屿的坐标分别为(a1,b1),(a2,b2)-----(an,bn),雷达的覆盖半径为r 求所有的岛屿都被覆盖所需要的最少的雷达数目. 首先将岛屿坐标进行处理,因为雷达的位置在x轴上,所以我们设雷达的坐标为(x,0),对于任意一个岛屿p(a,b),因为岛屿要满足在雷达的覆盖范围内,所以 (x-a)^2+b^2=r^2,解得 xmin=a-sqrt(r*r-b*b);//即为区间的左端点 xmax=a+sqrt(r*r-b*b);//即为区间的右端点 接下来区间选点即

poj 1328 Radar Installation(贪心+快排)

Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, s

poj 1328 Radar Installation 【贪心】【区间选点问题】

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54798   Accepted: 12352 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca