题解 UVA1193 Radar Installation
原题
思路
分析
因为半径d已经确定,所以对于每个点,我们可以算出它在x 轴上的覆盖位置线段LR,如图。

此问题便转为:
对于 n 个区间,每个区间内至少有1个点,求最少点数。
算法
我们可以将所有转化后的区间按左端点大小排序,然后记录上个点位置 las,对于每个点,有两种情况:
las < L , 我们必须再来一个点
las \(\ge\) L ,我们就可以尽量不再开点,而是使 las = \(\min\{las,R\}\) 来满足要求。
于是就可以贪心了!
代码
注意判无法达到情况。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#define db double
using namespace std;
const int MAXN = 1010;
int n,d,ans = 0,T = 0;
bool fail = 0;
struct island{
int x,y;
db l,r;
bool operator < (const island &b) const{
if(l == b.l) return r < b.r;
return l < b.l;
}
}isl[MAXN];
int main (){
while(scanf("%d %d",&n,&d) == 2){
if(n == 0) break;
T++;
fail = 0;
ans = 1;
if(d < 0) fail = 1;
double las;
for(int i = 0;i < n;i++){
scanf("%d %d",&isl[i].x,&isl[i].y);
if(isl[i].y > d ) fail = 1;
db temp = (db)( d * d - isl[i].y *isl[i].y );
if(temp < 0){ fail = 1; continue;}
temp = sqrt(temp);
isl[i].l = isl[i].x - temp;
isl[i].r = isl[i].x + temp;
}
printf("Case %d: ",T);
if(fail) {
printf("-1\n");
continue;
}
sort(isl+0,isl+n);
las = isl[0].r;
for(int i = 1;i < n;i++){
if(las < isl[i].l) {
ans++;
las = isl[i].r;
}
else las = min(las,isl[i].r);
}
printf("%d\n",ans);
}
return 0;
}
问题
- 在判断不可行时:
if(isl[i].x > d ) fail = 1;
- 在统计答案时:
if(las < isl[i].l) {
ans++;
las = isl[i].r;
}
//nothing
未写
else las = min(las,isl[i].r);
这样会保留不合法情况。
题解 UVA1193 Radar Installation的更多相关文章
- [POJ1328]Radar Installation
[POJ1328]Radar Installation 试题描述 Assume the coasting is an infinite straight line. Land is in one si ...
- poj 1328 Radar Installation【贪心区间选点】
Radar Installation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) ...
- Radar Installation(贪心,可以转化为今年暑假不ac类型)
Radar Installation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) ...
- POJ 1328 Radar Installation 贪心 A
POJ 1328 Radar Installation https://vjudge.net/problem/POJ-1328 题目: Assume the coasting is an infini ...
- POJ - 1328 Radar Installation(贪心区间选点+小学平面几何)
Input The input consists of several test cases. The first line of each case contains two integers n ...
- Radar Installation
Radar Installation 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/C 题目: De ...
- Radar Installation(贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 56826 Accepted: 12 ...
- 贪心 POJ 1328 Radar Installation
题目地址:http://poj.org/problem?id=1328 /* 贪心 (转载)题意:有一条海岸线,在海岸线上方是大海,海中有一些岛屿, 这些岛的位置已知,海岸线上有雷达,雷达的覆盖半径知 ...
- Radar Installation 分类: POJ 2015-06-15 19:54 8人阅读 评论(0) 收藏
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 60120 Accepted: 13 ...
随机推荐
- Beta冲刺<6/10>
这个作业属于哪个课程 软件工程 (福州大学至诚学院 - 计算机工程系) 这个作业要求在哪里 Beta冲刺 这个作业的目标 Beta冲刺--第六天(05.24) 作业正文 如下 其他参考文献 ... B ...
- Centos安装redis详解
redis官方网站对redis的安装已有介绍,但没有redis环境的安装,redis自动启动服务安装等等: redis官网:https://redis.io 本文以centos6为基础安装redis ...
- Github删除分支下所有提交记录
[本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究.若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!] 有时候,我们提交 ...
- 为页内的tab添加的iframe添加加载动画过渡效果
var iframe = $("iframe[data-id=" + id + " ]"); if (iframe.length > 0) { var e ...
- java 中对hashmap进行排序
public class HashMapSort { public static void main(String[] args) { HashMap<Integer, Student> ...
- Python函数参数详解
Python函数参数详解 形参与实参 什么是形参 在定义函数阶段定义的参数称之为形式参数,简称形参,相当于变量名. 什么是实参 在调用函数阶段传入的值称为实际参数,简称实参.相当于"变量值& ...
- python+opencv实现检测物体聚集区域
内容涉及:二值图像转换 / 检测连通区域面积 / 在原图上画框等 import cv2 import numpy as np for n in open('list.txt'): # list.txt ...
- Unicode 环境下的字符串的操作
1.CString转int int i _ttoi( str ); 2.保存中文和读取中文: CSdtioFile在Unicode环境下默认是不支持中文的,若需要存储和读取中文需要设置代码页: #in ...
- Ticket Game【博弈】
题目 Monocarp and Bicarp live in Berland, where every bus ticket consists of n digits (n is an even nu ...
- windows 下搭建 MQTT 服务
1.首先搭建起MQTT服务 1.1安装mosquitto,mosquitto是开源的MQTT代理服务器,它的Windows安装包地址:https://mosquitto.org/download/ 1 ...