POJ 1328 Radar Installation 贪心 A
POJ 1328 Radar Installation
https://vjudge.net/problem/POJ-1328
题目:
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
Examples
Input
3 2
1 2
-3 1
2 1 1 2
0 2 0 0
Output
Case 1: 2
Case 2: 1
分析:
写完AB之后发现真的不知道该怎么写C了。。。。几乎没什么坑现在还能踩了,于是又加一个A
题意很好理解,理解完之后直接干,用的二维贪心,,,
然后

mle。。。。Wawawa
从网上找的题解,都和我不同的思路????
wtf????
我的思路没毛病啊??
然后自己又仔细用数学证明了一遍,发现如果两个同样很远的点就会出现问题,我的贪心策略会使得灯塔++
emmmmmmmmm那就只能重打一遍了
错误代码:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <time.h>
#include <queue>
#include <string.h>
#define sf scanf
#define pf printf
#define lf double
#define ll long long
#define p123 printf("123\n");
#define pn printf("\n");
#define pk printf(" ");
#define p(n) printf("%d",n);
#define pln(n) printf("%d\n",n);
#define s(n) scanf("%d",&n);
#define ss(n) scanf("%s",n);
#define ps(n) printf("%s",n);
#define sld(n) scanf("%lld",&n);
#define pld(n) printf("%lld",n);
#define slf(n) scanf("%lf",&n);
#define plf(n) printf("%lf",n);
#define sc(n) scanf("%c",&n);
#define pc(n) printf("%c",n);
#define gc getchar();
#define re(n,a) memset(n,a,sizeof(n));
#define len(a) strlen(a)
#define LL long long
#define eps 1e-6
using namespace std;
struct A {
double x,y;
} a[];
int num = ;
int n;
double d;
int count0 = ;
bool cmp(A a, A b) {
if(a.x < b.x)
return true;
if(fabs(a.x - b.x) <= eps && a.y > b.y)
return true;
return false;
}
int f(int x) {
int xx = a[x].x + sqrt(d*d-a[x].y*a[x].y);
num ++;
for(int i = x+; i < n; i ++) { //p(i)
if(sqrt( a[i].y*a[i].y + (a[i].x-xx)*(a[i].x-xx) ) > d) {
f(i);
return ;
}
}
return ;
}
int main() {
while(~scanf("%d%lf",&n,&d) && (n != || d != 0.0)) {
num = ;
count0 ++;
int sta = ;
for(int i = ; i < n; i ++) {
slf(a[i].x) slf(a[i].y);
if(a[i].y > d) {
sta = ;
}
}
ps("Case ") p(count0) ps(": ");
if(sta == ) {
p(-) pn continue;
}
sort(a,a+n,cmp);
f();
p(num) pn
}
return ;
}
AC代码:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <time.h>
#include <queue>
#include <string.h>
#define sf scanf
#define pf printf
#define lf double
#define ll long long
#define p123 printf("123\n");
#define pn printf("\n");
#define pk printf(" ");
#define p(n) printf("%d",n);
#define pln(n) printf("%d\n",n);
#define s(n) scanf("%d",&n);
#define ss(n) scanf("%s",n);
#define ps(n) printf("%s",n);
#define sld(n) scanf("%lld",&n);
#define pld(n) printf("%lld",n);
#define slf(n) scanf("%lf",&n);
#define plf(n) printf("%lf",n);
#define sc(n) scanf("%c",&n);
#define pc(n) printf("%c",n);
#define gc getchar();
#define re(n,a) memset(n,a,sizeof(n));
#define len(a) strlen(a)
#define LL long long
#define eps 1e-6
using namespace std;
struct A {
double l,r;
int sta ;
} a[];
int n;
double d;
int count0 = ;
int num = ;
int count1 = ;
bool cmp(A a, A b) {
if(a.r!=b.r)
return a.r<b.r;
return a.l>b.l;
return false;
} int f(int x) {
count1 ++;
for(int i = x+; i < n; i++) {
if(a[i].l > a[x].r) {
f(i);
return ;
}
}
return ;
} int main() {
while(~scanf("%d%lf",&n,&d) && (n != || d != 0.0)) {
num = ;
count0 ++;
count1 = ;
int sta = ;
double x,y;
for(int i = ; i < n; i ++) {
slf(x) slf(y);
if(y > d) {
sta = ;
}
a[i].l = x-sqrt(d*d-y*y);
a[i].r = x+ sqrt(d*d-y*y);
}
ps("Case ")
p(count0)
ps(": ");
if(sta == ) {
p(-) pn
continue;
}
sort(a,a+n,cmp);
//f(0);
double end=-0x3f3f3f3f;//横坐标要很小....因为..你懂的
for(int i=; i<n; i++) {
if(end<a[i].l) {
end=a[i].r;
count1++;
}
} p(count1) pn
} return ;
}
POJ 1328 Radar Installation 贪心 A的更多相关文章
- 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 贪心 难度:1
http://poj.org/problem?id=1328 思路: 1.肯定y大于d的情况下答案为-1,其他时候必定有非负整数解 2.x,y同时考虑是较为麻烦的,想办法消掉y,用d^2-y^2获得圆 ...
- 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 ...
随机推荐
- SQL Server 2012无法连接到WMI提供程序
这篇文章主要介绍了SQL Server 2012无法连接到WMI提供程序(Cannot connect to WMI provider)解决方案,需要的朋友可以参考下 今天一位同事在启动自己工作机的S ...
- vuex状态管理-数据改变不刷新
困惑: 在页面初始化的时候,我提交到vuex状态管理,然后在获取的时候获取不到,我找到了出错的地点,并进行了修改,然后可以获取到状态 但是不知道原因? 定义了如下的state const state ...
- Vue Checkbox全选和选中的方法
<div class="search-content"> <Checkbox :value="checkAll" @click.prevent ...
- mysql 表映射为java bean 手动生成。
在日常工作中,一般是先建表.后建类.当然也有先UML构建类与类的层级关系,直接生成表.(建模)这里只针对先有表后有类的情况.不采用代码生成器的情况. 例如: 原表结构: ),)) BEGIN ); ) ...
- django 过滤器,标签
过滤器: <p>{{ date|date:"Y-m-d" }}</p> {#2018-05-28,date是当前时间#} <p>{{ l|len ...
- python如何打开一个大文件?
with open('a.csv','r') as f: for i in f: print(i) while True: a = f.readline() if not a: break f.rea ...
- 使用swiper插件,隐藏swiper后再显示,不会触发自动播放的解决办法
问题: 项目中有一个需求,当点击P1时,两个页面进行轮播.当点击P2时,页面不轮播. 设置好以后,点击P2,再点击P1,此时页面不能自动轮播,只能手动触发. 解决: 在轮播器配置里,配置observe ...
- 百度分享不支持Https的解决方案--本地化
站点自从开启 https 之后 ,百度分享就不能用了!但是又寻找不到类似百度分享的替代品.. 怎么办呢?要如何解决 百度分享不支持https的问题呢, 跟着博主动动手,让你百度分享仍然能在https下 ...
- Linux jdk安装
Linux上一般会安装Open JDK,关于OpenJDK和JDK的区别:http://www.cnblogs.com/sxdcgaq8080/p/7487369.html 下面开始安装步骤: --- ...
- VueJs学习参考的例子
his is a vue+mint's demo ,for loler(PAD LOL) https://github.com/yuanman0109/vue2.0-Mint-lolbox An ...