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 ...
随机推荐
- 在WINDOWS下安装MYSQL8.0
1:创建文件夹D:\data\service\mysql-8.0.11-winx64\data 2:进到D:\data\service\mysql-8.0.11-winx64\bin 第三步:初始化. ...
- jmeter分布式、linux运行
一.jmeter分布式压测(多台电脑一起压测) 1.有多台电脑,每台电脑上都有jmeter,而且这几台电脑都互相能ping通 2.在我的电脑的jmeter,bin目录下,修改jmeter.proper ...
- Ajax请求的几个小练习
Ajax请求的几个小练习 准备工作 路由中做了分发: re_path('^app01/',include('app01.url')) app01中url.py文件的内容: from django.ur ...
- VRay材质练习(一):水、玻璃、牛奶
软件环境 a) 3ds max 2014b) V-Ray 3.60.03 渲染效果图集 玻璃杯 玻璃杯+水 玻璃杯+牛奶 材质详细参数 一.玻璃材质 Diffuse (0,0,0), Roughnes ...
- Linux背背背(3)
目录 1.文件操作命令 2.文件夹操作命令 文件操作命令 创建 命令:touch 语法:#touch 文件的名字 文件名可以是一个完整的路径 如果后面的参数文件名指定了路径,则表示在指定的路 ...
- Problem B: 取石子
转换成一个数在(0,X + Y)的加减问题 考虑一种使用线段树处理的方法, 维护前缀最大值, 前缀最小值, 前缀和, 然后查询的时候先询问右区间是否会同时碰到上下界, 会的话左区间无用直接递归右区间, ...
- day39数据库之基本数据类型
数据库之数据类型1.数据存储引擎 一个功能的核心部分,回到mysql 核心功能是存储数据 涉及到存储数据的代码 就称之为存储引擎 根据不同的需求 也有着不同的引擎分类 不 ...
- Celery 异步任务 , 定时任务 , 周期任务 的芹菜
1.什么是Celery?Celery 是芹菜Celery 是基于Python实现的模块, 用于执行异步定时周期任务的其结构的组成是由 1.用户任务 app 2.管道 broker 用于存储 ...
- 彻底征服 Spring AOP 之 实战篇
Spring AOP 实战 看了上面这么多的理论知识, 不知道大家有没有觉得枯燥哈. 不过不要急, 俗话说理论是实践的基础, 对 Spring AOP 有了基本的理论认识后, 我们来看一下下面几个 ...
- linux 时间和时区设置
在linux中与时间相关的文件有 /etc/localtime /etc/timezone 其中,/etc/localtime是用来描述本机时间,而 /etc/timezone是用来描述本机所属的时区 ...