POJ1328(贪心)

题目

Radar Installation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 150055 Accepted: 33255
Description
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
For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. “-1” installation means no solution for that case.

题意

在x轴的上方有若干个小岛,可以在x轴上布置雷达,给出了雷达的覆盖范围,要我们找出布置雷达的最小数量。

分析

第一反应是直接使用贪心,将小岛按照x坐标由小到大排序,从最左边的小岛开始,设定一个距离它最远的可以覆盖到该岛的雷达。遍历所有小岛,当前小岛无法被上一个雷达覆盖,则重复第一次操作。
敲代码一气呵成,然后成功wa了。(+﹏+)
冥思苦想后,发现问题出在排序的方法上,不应该按x坐标进行排序,x坐标最左边的小岛对应的雷达可能不是真正最左的那个,还要考虑y轴因素,所以直接计算每个小岛对应的雷达位置,按照雷达位置给小岛排序。

代码

第一次错误代码

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio> using namespace std;struct gg
{int x, y;bool operator < (const gg a)const{return x < a.x;}
};int main()
{int n, d, num = 0;while (scanf("%d%d", &n, &d) && (n || d)){num ++ ;bool flag = false;gg g[1010];for (int i = 0; i < n; i ++ ){scanf("%d%d", &g[i].x, &g[i].y);if (g[i].y > d) flag = true;}if (flag){printf("Case %d: -1\n", num);continue;}sort(g,g + n);double z = (double)(sqrt(d*d - g[0].y*g[0].y) + g[0].x);int res = 1;for (int i = 0; i < n; i ++ ){int x = g[i].x, y = g[i].y;if ((double)((x - z)*(x - z) + y*y) <= d*d) continue;else{z = (double)(sqrt(d*d - y*y) + x);res ++ ;}}printf("Case %d: %d\n", num, res);}return 0;
}

正确代码

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio> using namespace std;struct gg
{int x, y;double leida;bool operator < (const gg a)const{return leida < a.leida;}
};int main()
{int n, d, num = 0;while (scanf("%d%d", &n, &d) && (n || d)){num ++ ;bool flag = false;gg g[1010];for (int i = 0; i < n; i ++ ){scanf("%d%d", &g[i].x, &g[i].y);if (g[i].y > d) flag = true;}if (flag){printf("Case %d: -1\n", num);continue;}for (int i = 0; i < n; i ++ ){g[i].leida = (double)(sqrt(d*d - g[i].y*g[i].y) + g[i].x);}sort(g,g + n);double z = g[0].leida;int res = 1;for (int i = 0; i < n; i ++ ){int x = g[i].x, y = g[i].y;if ((double)((x - z)*(x - z) + y*y) <= d*d) continue;else{z = g[i].leida;res ++ ;}}printf("Case %d: %d\n", num, res);}return 0;
}

POJ1328(贪心)题解相关推荐

  1. POJ1328贪心放雷达

    题意:        有一个二维坐标,y>0是海,y<=0是陆地,然后只能在y=0的岸边上放雷达,有n个城市需要被监控,问最少放多少个雷达. 思路:       贪心去做就行了,其实题目不 ...

  2. C++奥赛一本通贪心题解

    C++奥赛一本通刷题记录(贪心) 2017.11.15 By gwj1139177410 书不见了,占坑待填. An Easy Problem poj2453 //贪心, 将最右边第一个01改成10并 ...

  3. 零件加工 贪心 题解

    1019: B06-贪心-零件加工[提高组] 时间限制: 1 Sec  内存限制: 128 MB 提交: 24  解决: 7 [提交] [状态] [讨论版] [命题人:外部导入] 题目描述 工匠小K最 ...

  4. 数据结构——马踏棋盘题解(贪心算法)

    本文转自: https://blog.csdn.net/qq_41596568/article/details/83060317 数据结构--马踏棋盘题解(贪心算法) 使用循环建立棋盘与权值棋盘(权值 ...

  5. 数据结构——马踏棋盘题解(贪心算法)(C语言)

    数据结构--马踏棋盘题解(贪心算法) 使用循环建立棋盘与权值棋盘(权值为该位置可走的位置数量) 将当前步数写入棋盘数组中 开始探测下一步该走的位置, 分别测试八个方向 对可走位置进行查询权值,将权值最 ...

  6. 牛客题霸 [ 最长递增子序列] C++题解/答案

    牛客题霸 [ 最长递增子序列] C++题解/答案 题目描述 给定数组arr,设长度为n,输出arr的最长递增子序列.(如果有多个答案,请输出其中字典序最小的) 题意: 直接暴力会超时 应该用二分+贪心 ...

  7. AGC001 补题小结

    Problem A BBQ Easy 简要题意:给定 \(2n\) 个数字,两两配对,一对的贡献为它们的 \(min\) ,求最大贡献和.\(n\le 100\) tag:小学生贪心 题解:排个序,求 ...

  8. 【GDOI2018】所有题目和解题报告

    使用说明:题意和数据范围都只是回忆内容,仅供参考.题解陆续补上. Day 1 第一题 题意:给定n个数字,要求划分成k的连续段使得每个连续段内的数字之和相同,求最大的k.n,Σai<=10^6. ...

  9. 金山中学 rugular SRM 04 ——纪念我的第一次Ak

    虽然只是一场比较简单的比赛 但奈何我也比较弱啊.... T1 一道计算概率的题目 T SRM 04 描述 给个长度为 n 的数列,每次操作能将数列打乱(RandomShuffle),问在期望下需要多少 ...

  10. 线段树的进阶:多种信息的维护与传递

     山海经 时间限制:1 s   内存限制:128 MB [问题描述] "南山之首曰鹊山.其首曰招摇之山,临于西海之上,多桂,多金玉.有草焉,其状如韭而青华,其名曰祝余,食之不饥--又东三百里 ...

最新文章

  1. RESTful风格及其SpringMVC实现
  2. kissy core
  3. 【译】使用Kotlin和RxJava测试MVP架构的完整示例 - 第1部分
  4. restful可以转发么_什么是RESTFUL?REST的请求方法有哪些,有什么区别?
  5. VS Code的7个开源替代品
  6. asp开发中存储过程应用全接触 _asp技巧
  7. python用字典编写购物程序_Python编写购物小程序
  8. 共享x轴,使用双y轴
  9. java对象赋值_Java 对象不使用时为什么要赋值为 null?
  10. SAP CRM WebClient UI上分销渠道点击展开按钮后执行了哪些逻辑
  11. java 和 区别_java 和 =的区别
  12. 最优化学习笔记(一)——牛顿法(一维搜索方法)
  13. 使用PostBackUrl属性实现跨页面传值
  14. 不重启程序使用最新版package
  15. 军犬舆情热点:最高检明确正当防卫标准;ofo戴威称勇敢活下去
  16. C#DataGridView使用线程定时循环滚动数据
  17. 计算机考研数学考数学二的专业,考研常识:哪些专业考数学二?
  18. 莺尾花数据集–贝叶斯分类(day5)
  19. hostapd实现WIFI 热点(AP)
  20. Navicat连接mysql时出现 Access denied for user ‘root‘@‘xxx.xxx.xxx.xxx‘ (using password: YES) 的原因及解决办法。

热门文章

  1. Linux:MLX90614驱动
  2. 将一个文件伪装在另一个文件下
  3. 【3】Kali破解家用WI-FI密码 - 建立伪装热点
  4. 网易不进垃圾箱html,腾讯QQ、网易126、163邮箱发送邮件进入垃圾箱及收不到邮件怎么办?...
  5. 一加手机怎么root权限_一加手机的两种ROOT权限获取教程详解
  6. iOS经典讲解之播放本地音频文件
  7. 考研政治刷题知识点总结
  8. python有理数_Python3标准库:fractions有理数
  9. Radon变换主要知识点
  10. 关于deepin-wine或wine更换字体方法