POJ 1328 Radar Installation 贪心 难度:1
http://poj.org/problem?id=1328
思路:
1.肯定y大于d的情况下答案为-1,其他时候必定有非负整数解
2.x,y同时考虑是较为麻烦的,想办法消掉y,用d^2-y^2获得圆心允许范围,问题转化为在许多圆心允许范围内取尽可能少的点,也即在许多线段上取尽可能少的点,使得所有线段上都有点被取到
3.从左往右考虑,完全在左边的线段肯定要取点,如果这个点在当前线段上已经取了,明显就可以忽略当前线段,明显在线段上的最优点是右端点
#include <iostream>
#include <cmath>
using namespace std; typedef pair<double,double> p;
p pos[];
p temp[];
int n,d; int MERGE(int s,int e){
int mid=(e+s)/;
int i=s,j=mid,k=s;
while(i<mid&&j<e){
if(pos[i].first>pos[j].first||(pos[i].first==pos[j].first&&pos[i].second>pos[j].second)){
temp[k].first=pos[j].first;
temp[k].second=pos[j].second;
k++;
j++;
}
else {
temp[k].first=pos[i].first;
temp[k].second=pos[i].second;
k++;
i++;
}
}
int kk=k;
for(;i<mid;i++){
pos[kk].first=pos[i].first;
pos[kk].second=pos[i].second;
kk++;
}
for(int ii=s;ii<k;ii++){
pos[ii].first=temp[ii].first;
pos[ii].second=temp[ii].second;
}
return ;
}
int MERGE_SORT1(int s,int e){
if(e-s<){
return ;
}
int mid=(s+e)/;
MERGE_SORT1(s,mid);
MERGE_SORT1(mid,e);
MERGE(s,e);
return ;
} int solve(){
double x,y;
double sq;
bool flag=false;
for(int i=;i<n;i++){
cin>>x>>y;
if(y>d){
flag=true;
continue;
}
if(y<){
i--;
n--;
continue;
}
sq=sqrt((double)(d*d-y*y));
pos[i].first=x-sq;
pos[i].second=x+sq;
}
if(flag)return ;
MERGE_SORT1(,n);
int ans=;
double mx;
mx=pos[].second;
for(int i=;i<n;i++){
if(mx<pos[i].first){
ans++;
mx=pos[i].second;
}
else if(mx>pos[i].second){
mx=pos[i].second;
}
}
return ans;
} int main(){
int ci=;
while(ci++){
cin>>n>>d;
if(n<=)break;
int ans=solve();
if(ans){
cout<<"Case "<<ci-<<":"<<" "<<ans<<endl;
}
else{
cout<<"Case "<<ci-<<":"<<" "<<-<<endl;
}
}
return ;
}
POJ 1328 Radar Installation 贪心 难度:1的更多相关文章
- 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(贪心+快排)
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- POJ - 1328 Radar Installation(贪心区间选点+小学平面几何)
Input The input consists of several test cases. The first line of each case contains two integers n ...
- POJ 1328 Radar Installation 贪心算法
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- poj 1328 Radar Installation(贪心)
题目:http://poj.org/problem?id=1328 题意:建立一个平面坐标,x轴上方是海洋,x轴下方是陆地.在海上有n个小岛,每个小岛看做一个点.然后在x轴上有雷达,雷达能覆盖的范 ...
- POJ 1328 Radar Installation 贪心题解
本题是贪心法题解.只是须要自己观察出规律.这就不easy了,非常easy出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 依照x轴大小排序 2 从最左边的点循环.首先找到最小x轴的圆 ...
- POJ 1328 Radar Installation#贪心(坐标几何题)
(- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<algorithm> #include<cmath ...
- 贪心 POJ 1328 Radar Installation
题目地址:http://poj.org/problem?id=1328 /* 贪心 (转载)题意:有一条海岸线,在海岸线上方是大海,海中有一些岛屿, 这些岛的位置已知,海岸线上有雷达,雷达的覆盖半径知 ...
- poj 1328 Radar Installation (简单的贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42925 Accepted: 94 ...
随机推荐
- 半平面交模板(O(n*n)&& O(n*log(n))
摘自http://blog.csdn.net/accry/article/details/6070621 首先解决问题:什么是半平面? 顾名思义,半平面就是指平面的一半,我们知道,一条直线可以将平面分 ...
- Android生命周期和Service生命周期
android生命周期 运行:oncreate → onstart → onresume暂停:onresume → onpause:再次运行:onresume停止:onpause → onstop → ...
- Javascript设计模式之匿名函数与闭包
匿名函数 (function () { var foo = 10; var bar = 2; console.log(foo*bar); })(); // 20 带参数的匿名函数 (function ...
- JS---------->数组练习!
var arr = [4, 0, 7, 9, 0, 0, 2, 6, 0, 3, 1, 0]; 要求将数组中的0项去掉,将不为0的值存入一个新的数组,生成新的数组 <!doctype htm ...
- [Selenium] 使用Javascript选中Input框里的内容,然后清空
当我们需要清空Input框里的内容,直接使用el.clear()方法又行不通时,可以使用Javascript先去选中内容,然后再使用el.clear()方法:
- markdown 语法学习
markdown是一种轻量级的标记语言,和html类似,但是易读性比html要好很多. 基本语法: 1.标题和正文分隔线 === //至少三个连续等号,最高阶标题 --- //或者三个连续的减号,第二 ...
- opencl gauss filter优化(二)
1.buffer使用image的方式:Horizontal 与 Vertical 算法一样, 共需30ms,wait time 19ms. const sampler_t sampler = CLK_ ...
- drupal_get_path_alias
drupal_get_path_alias('path','language')这个函数是去读取url_alias表,获得某个path在特定language下的alias列的值--刚开始我 dpm(d ...
- CRLF CSRF XSS
http://baike.baidu.com/link?url=BXWN2I6J23thrrm6JoEnAYvmNqp83llyaydaj5RYkq--tuJKSFuMuDMIoTPnKjthRUm3 ...
- SyncServer obj