【贪心】「poj1328」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, so an island in the sea can be covered by a radius installation, if the distance between them is at most d. 

We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates. 
 
Figure A Sample Input of Radar Installations


Input

The input consists of several test cases. The first line of each case contains two integers n (1<=n<=1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases. 

The input is terminated by a line containing pair of zeros 

Output

For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. "-1" installation means no solution for that case.

Sample Input

3 2
1 2
-3 1
2 1

1 2
0 2

0 0

Sample Output

Case 1: 2
Case 2: 1

 

鄙人仍然码力不够……建模虽然很快就建出来了,但是写了老半天交了近十发才磕磕碰碰A掉此题。

先来分析一下吧

题意

有n个在一二象限上的整点,要求在x轴上选取ans个点,满足以每个点为圆的图形并集包含n个整点且ans最小。

分析

因为这里每个监测站的有效范围为圆形,我们可以反过来以n个整点为圆心,d为半径作圆。每个圆在x轴上有两个交点,即映射后的线段,那么在这条线段上至少要有一个监测站。想到这里我马上就想起以前做过的一道叫做“监测站”的题目,于是很快开写了。

CE第一发

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 struct seg{int l,r;}f[1003];
 8 bool cmp(seg a, seg b){return a.r<b.r;}
 9 int n,d,ans;
10 inline void mapping(int x, int y, int i)
11 {
12     int ss = (int)sqrt(d*d-y*y);
13     f[i].l = x-ss;
14     f[i].r = x+ss;
15     return;
16 }
17 inline void work()
18 {
19     memset(f, 0, sizeof(f));
20     ans = -1;
21     for (int i=1; i<=n; i++)
22     {
23         int x,y;
24         scanf("%d%d",&x,&y);
25         if (y > d)return;
26         mapping(x, y, i);
27     }
28     ans = 0;
29     sort(f+1, f+n+1, cmp);
30     int i = 1;int j = 1;
31     while (j<=n)
32     {
33         ans++;
34         while(f[j].l<=f[i].r&&j<=n)j++;
35         i = j;
36     } 
37     return;
38 }
39 int main()
40 {
41     scanf("%d%d",&n,&d);
42     while (n!=0&&d!=0)
43     {
44         work();
45         printf("%d\n",ans);
46         scanf("%d%d",&n,&d);
47     }
48     return 0;
49 }
View Code

然后,愉快快快快快快快地CE了:)

woc在poj上面sqrt(int)会爆我的天哪。

解决方法:

  1.$sqrt(int * 1.0)$

  2.$sqrt((double) int)$

  3.$sqrtf(int)$

WA第一发

  隐隐发觉:似乎监测点可以不在整点上?把segment的l,r改成double

  然而不有这个错

WA第二发

  手算一组数据发现线段要按左端点排序

  然而不有这个错

WA第三发

  逐条check时候更新不对

  第三发的

WA第四发

  第四发交的

  好的吧显而易见第四发也是错的

WA第五发

  已经心态爆炸

  找了一组讨论区里的数据跑了跑,发现!

  我在判不可能情况时候就在子过程里return了

  但这是多组数据啊!没读完的被当成下组数据读进去了……

  还有,我一直把输出里的"Case"当成看看的……(毕竟有些USACO的题不就这样么)

  没想到它居然是要输出的:)

AC这一发

  

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 struct seg{double l,r;}f[1003];//WA-1
 8 bool cmp(seg a, seg b){return a.l-b.l<1E-5;}//WA-2
 9 int n,d,ans;
10 inline void mapping(int x,int y, int i)
11 {
12     double ss = sqrt(d*d*1.0-y*y*1.0);
13     f[i].l = x*1.0-ss;
14     f[i].r = x*1.0+ss;
15     return;
16 }
17 inline void work()
18 {
19     memset(f, 0, sizeof(f));
20     ans = -1;
21     bool fl = 0;
22     for (int i=1; i<=n; i++)
23     {
24         int x,y;
25         scanf("%d%d",&x,&y);
26         if (y > d)fl = 1;
27         mapping(x, y, i);
28     }if (fl)return;
29     ans = 0;
30     sort(f+1, f+n+1, cmp);
31     int i = 1;int j = 1;
32     while (j<=n)
33     {
34         ans++;
35         while((f[j].l-f[i].r<=1E-5)&&j<=n)
36         {
37             j++;
38             if (f[j].r-f[i].r < 1E-5)i = j;    //WA-3 WA-4
39         }
40         i = j;
41     } 
42     return;
43 }
44 int main()
45 {
46     scanf("%d%d",&n,&d);
47     int t = 0;
48     while (n!=0||d!=0)
49     {
50         t++;
51         work();
52         printf("Case %d: %d\n",t,ans);  //WA-5
53         scanf("%d%d",&n,&d);
54     }
55     return 0;
56 }

 

  代码能力差还得多写题啊~

 

转载于:https://www.cnblogs.com/antiquality/p/8508539.html

标题基于SpringBoot的马术俱乐部管理系统设计与实现AI更换标题第1章引言介绍马术俱乐部管理系统的研究背景、意义、国内外研究现状、论文方法及创新点。1.1研究背景与意义阐述马术俱乐部管理系统对提升俱乐部管理效率的重要性。1.2国内外研究现状分析国内外马术俱乐部管理系统的发展现状及存在的问题。1.3研究方法以及创新点概述本文采用的研究方法,包括SpringBoot框架的应用,以及系统的创新点。第2章相关理论总结和评述与马术俱乐部管理系统相关的现有理论。2.1SpringBoot框架理论介绍SpringBoot框架的基本原理、特点及其在Web开发中的应用。2.2数据库设计理论阐述数据库设计的基本原则、方法以及在管理系统中的应用。2.3马术俱乐部管理理论概述马术俱乐部管理的基本理论,包括会员管理、课程安排等。第3章系统设计详细描述马术俱乐部管理系统的设计方案,包括架构设计、功能模块设计等。3.1系统架构设计给出系统的整体架构,包括前端、后端和数据库的交互方式。3.2功能模块设计详细介绍系统的各个功能模块,如会员管理、课程管理、预约管理等。3.3数据库设计阐述数据库的设计方案,包括表结构、字段设计以及数据关系。第4章系统实现介绍马术俱乐部管理系统的实现过程,包括开发环境、编码实现等。4.1开发环境搭建介绍系统开发所需的环境,包括操作系统、开发工具等。4.2编码实现详细介绍系统各个功能模块的编码实现过程。4.3系统测试与调试阐述系统的测试方法、测试用例以及调试过程。第5章系统应用与分析呈现马术俱乐部管理系统的应用效果,并进行性能分析。5.1系统应用情况介绍系统在马术俱乐部中的实际应用情况。5.2系统性能分析从响应时间、并发处理能力等方面对系统性能进行分析。5.3用户反馈与改进收集用户反馈,提出系统改进建议。第6章结论与展望总结马术俱乐部管理系统的设计与实现成果,并展望未来的研究
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值