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 ...
随机推荐
- sitemesh使用
参考文章: https://my.oschina.net/heroShane/blog/199177 http://blog.csdn.net/u013019926/article/details/1 ...
- flutter学习地址
Flutter - 不一样的跨平台解决方案: 关于Flutter,你想知道的都在这里了!: Flutter 时间表 2015 年 4 月,Flutter(最初代号 Sky)在 Dart Devel ...
- Lepus监控之Oracle配置
1.安装cx_Oracle a.官网下载客户端组件包 oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpmoracle-instantclien ...
- kubernetes学习笔记之十二:资源指标API及自定义指标API
第一章.前言 以前是用heapster来收集资源指标才能看,现在heapster要废弃了从1.8以后引入了资源api指标监视 资源指标:metrics-server(核心指标) 自定义指标:prome ...
- 7-安装Spark
1.Apache Hadoop2.7中的YARN与JAVA8有冲突,如果想要使用spark on yarn,首先需要在yarn-site.xml中配置如下项: <property> < ...
- 移动端使用mint-ui loadmore实现下拉刷新上拉显示更多
前序:在使用vue做一个h5项目的时候,需要上拉分页加载,实践中总结一下: 首先要安装mint-ui npm i mint-ui -S 然后引入,一般在main.js里面 import Vue fro ...
- vue slot
一直觉得vue的slot比较申请,而且比较深奥,总有点不想用的感觉,事实上,在一定程度上,也真的可以完全避开slot就能把一个项目完全搭建完成. 但是随着用的次数越来越多,看到的内容也越来越多的情况, ...
- mac无密登陆到linux
最近弄了台linux云服务器,然而每次登陆linux都好麻烦,所以倒腾了下ssh无密登陆. linux:centos 6.5,自带ssh mac:yosemite,自带ssh 步骤: 1. 创建key ...
- 买二手iphone的建议
手机到手后一定要在第一时间把“按键开关.指纹解锁.指南针.照相机.话筒.听筒.手电筒.定位.WiFi”都测一遍. 环境有无wifi:imei:×××××序列号:××××× 外观和零件:1外观,1是否粗 ...
- leetcode4
public class Solution { public double FindMedianSortedArrays(int[] nums1, int[] nums2) { var nums = ...