fzuoj1111Radar Installation (贪心)

题目大意是在海岸线布置n个雷达,要求雷达的范围要包含所有的小岛;

思路:逆向思维把小岛看成一个个范围,与海岸线的交集,从最左端的开始找 (贪心最左端的点),接着不用一个一个去遍历,直接用前一个的右端点去替换下一个的左端点。。。。直至最后一个点。大致思想就是贪心,还是比较正常的题,适合刚学c语言的新生做(小白我就是一枚)。

下面是代码:

 1 #include <iostream>
 2 #include <cmath>
 3 #include <algorithm>
 4 #include <cstdio>
 5 using namespace std;
 6 struct node
 7 {
 8     double left,right;
 9 }island[1001];
10 bool cmp(node a,node b)
11 {
12     return a.left < b.left;
13 }
14 int main()
15 {
16     double x,y,d,temp;
17     int i,cnt,n,k=0;
18     bool flag;
19     while(scanf("%d%lf",&n,&d)&&!(n==0&&d==0))
20     {
21         k++;
22         flag=false;
23         for(i=0; i<n; i++)
24         {
25             scanf("%lf%lf",&x,&y);
26             if(y > d||d<0)
27             {
28                 flag = true;
29             }
30             island[i].right = x+sqrt(d*d-y*y);///岛屿右端点初始化
31             island[i].left = x-sqrt(d*d-y*y);///岛屿左端点初始化
32         }
33         if(flag)
34         {
35             printf("Case %d: -1\n",k);
36             continue;
37         }
38         sort(island,island+n,cmp);
39         temp=island[0].right;
40         cnt=1;
41         for(i=1; i<n; i++)
42         {
43             if(island[i].right <= temp)
44             {
45                 temp = island[i].right;///岛屿右端点的替换
46             }
47             else if(island[i].left > temp)
48             {
49                 cnt++;
50                 temp = island[i].right;
51             }
52         }
53         printf("Case %d: %d\n",k,cnt);
54     }
55     return 0;
56 }
时间: 2024-10-13 02:08:04

fzuoj1111Radar Installation (贪心)的相关文章

poj1328Radar Installation(贪心—区间选点)

题目链接: 啊哈哈,点我点我 题目: Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52037   Accepted: 11682 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small isl

POJ 1328、Radar Installation 贪心

jQuery的属性操作非常简单,下面以一个a元素来说明属性的获取/设置/删除操作 <body> <a>jquery.com</a> </body> 添加属性 $('a').attr('href', 'http://www.jquery.com') 添加多个属性 $('a').attr({'href':'http://www.jquery.com', 'title':'jquery.com'}) 获取属性 $('a').attr('href') class属性

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

poj1328Radar Installation 贪心

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 64472   Accepted: 14497 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

Radar Installation(贪心)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54295   Accepted: 12208 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(贪心+快排)

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

Radar Installation(贪心,可以转化为今年暑假不ac类型)

Radar Installation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Submission(s) : 54   Accepted Submission(s) : 28 Problem Description Assume the coasting is an infinite straight line. Land is in one side of co