建模:二维转一维;贪心

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第一发

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct seg{int l,r;}f[];
bool cmp(seg a, seg b){return a.r<b.r;}
int n,d,ans;
inline void mapping(int x, int y, int i)
{
int ss = (int)sqrt(d*d-y*y);
f[i].l = x-ss;
f[i].r = x+ss;
return;
}
inline void work()
{
memset(f, , sizeof(f));
ans = -;
for (int i=; i<=n; i++)
{
int x,y;
scanf("%d%d",&x,&y);
if (y > d)return;
mapping(x, y, i);
}
ans = ;
sort(f+, f+n+, cmp);
int i = ;int j = ;
while (j<=n)
{
ans++;
while(f[j].l<=f[i].r&&j<=n)j++;
i = j;
}
return;
}
int main()
{
scanf("%d%d",&n,&d);
while (n!=&&d!=)
{
work();
printf("%d\n",ans);
scanf("%d%d",&n,&d);
}
return ;
}

然后,愉快快快快快快快地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这一发

  

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct seg{double l,r;}f[];//WA-1
bool cmp(seg a, seg b){return a.l-b.l<1E-;}//WA-2
int n,d,ans;
inline void mapping(int x,int y, int i)
{
double ss = sqrt(d*d*1.0-y*y*1.0);
f[i].l = x*1.0-ss;
f[i].r = x*1.0+ss;
return;
}
inline void work()
{
memset(f, , sizeof(f));
ans = -;
bool fl = ;
for (int i=; i<=n; i++)
{
int x,y;
scanf("%d%d",&x,&y);
if (y > d)fl = 1;
mapping(x, y, i);
}if (fl)return;
ans = ;
sort(f+, f+n+, cmp);
int i = ;int j = ;
while (j<=n)
{
ans++;
while((f[j].l-f[i].r<=1E-)&&j<=n)
{
j++;
if (f[j].r-f[i].r < 1E-)i = j;    //WA-3 WA-4
}
i = j;
}
return;
}
int main()
{
scanf("%d%d",&n,&d);
int t = ;
while (n!=||d!=)
{
t++;
work();
printf("Case %d: %d\n",t,ans);  //WA-5
scanf("%d%d",&n,&d);
}
return ;
}

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

【贪心】「poj1328」Radar Installation的更多相关文章

  1. 「个人训练」Radar Installation(POJ-1328)

    这条题目A了十次...emmmmm 其实不难就是一个贪心.... 先说下算法(之前的和现在的) 之前考虑的其实很简单.用平面几何即可将雷达可以放置的区域转化为区间(顺便判断是否无解.问题就比较简单了: ...

  2. POJ--1328 Radar Installation(贪心 排序)

    题目:Radar Installation 对于x轴上方的每个建筑 可以计算出x轴上一段区间可以包含这个点 所以就转化成 有多少个区间可以涵盖这所有的点 排序之后贪心一下就ok 用cin 好像一直t看 ...

  3. POJ1328 Radar Installation 【贪心&#183;区间选点】

    Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54593   Accepted: 12 ...

  4. [POJ1328]Radar Installation

    [POJ1328]Radar Installation 试题描述 Assume the coasting is an infinite straight line. Land is in one si ...

  5. POJ 1328 Radar Installation 贪心 A

    POJ 1328 Radar Installation https://vjudge.net/problem/POJ-1328 题目: Assume the coasting is an infini ...

  6. 贪心 POJ 1328 Radar Installation

    题目地址:http://poj.org/problem?id=1328 /* 贪心 (转载)题意:有一条海岸线,在海岸线上方是大海,海中有一些岛屿, 这些岛的位置已知,海岸线上有雷达,雷达的覆盖半径知 ...

  7. POJ1328——Radar Installation

    Radar Installation Description Assume the coasting is an infinite straight line. Land is in one side ...

  8. poj 1328 Radar Installation(贪心)

    Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...

  9. poj 1328 Radar Installation (简单的贪心)

    Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42925   Accepted: 94 ...

随机推荐

  1. 【渗透测试】如何利用burpsuite测试无回显漏洞

    前面的文章讲了在windows和linux上的不同的无文件渗透测试的方法,那么这篇文章给大家讲解如何在漏洞没有回显的情况下,利用burpsuite自带插件进行测试的方式. 首先我们稍微提一下有哪些无回 ...

  2. button 获取 cell

        - (void)cellBtnClicked:(id)sender event:(id)event {     NSSet *touches =[event allTouches];      ...

  3. 关于JS中的call()方法和apply() 暂时只接触到call() 等接触到apply()再回头来看

    1. 每个函数都包含两个非继承而来的方法:call()方法和apply()方法. 2. 相同点:这两个方法的作用是一样的. 都是在特定的作用域中调用函数,等于设置函数体内this对象的值,以扩充函数赖 ...

  4. UVa12304(计算几何中圆的基本操作)

    断断续续写了250多行的模拟,其间被其他事情打扰,总共花了一天才AC吧~ 这道题目再次让我明白,有些事情看起来很难,实际上并没有我们想象中的那么难.当然了我主要指的不是这个题的难度…… 也是初学计算几 ...

  5. LM358与TL431验证

  6. [已读]ppk谈javascript

    读的第一本javascript方面的书籍,印象也比较深.ppk对浏览器兼容很有研究~~可以看看他的www.quirksmode.org

  7. centos7安装mysql5.7 使用yum

    https://blog.csdn.net/z13615480737/article/details/78906598 使用yum,比较简单,不用考虑版本依赖问题

  8. android开发学习 ------- debug 和 release版本执行结果不同

    在debug上测试成功的,release上测试不成功,就想着怎么将 release 版本进行调试一下.还好 Android Studio 3.0是可以进行调试apk的 可以显示log,自己看自己的逻辑 ...

  9. 实现如下语法的功能:var a = add(2)(3)(4)

    function add(num){ var _add = function(args){ num+=args; return arguments.callee; } _add.toString = ...

  10. JS权威指南-概述学习

    <script src="/javascripts/application.js" type="text/javascript" charset=&quo ...