Radar Installation -poj 1328
2015-06-24 19:43
302 查看
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 60277 | Accepted: 13583 |
OutputFor 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.
Sample Input
3 2 1 2 -3 1 2 1 1 2 0 2 0 0
Sample Output
Case 1: 2 Case 2: 1 使用贪心算法解决。
#include <iostream> #include<math.h> #include <algorithm> #include <stdlib.h> using namespace std; struct point { double left, right; }points[1000], temp; bool operator < (point a, point b) { return a.left < b.left; } int main() { int num = 0; double rad = 0; cin >> num >> rad; int casenum=1; while (num || rad) { int rnum=1; int wrong = 0; for (int i = 0; i < num; i++) { double x=0; double y=0; cin >> x >> y; if (y > rad) { //岛屿纵坐标大于雷达半径 wrong = 1; }else{ //以岛屿为圆心,做圆,与x轴的交点 points[i].left=x-sqrt((rad*rad-y*y)*1.0); points[i].right=x+sqrt((rad*rad-y*y)*1.0); } } if (wrong) { //雷达不能覆盖 cout<<"Case "<<casenum++<<": "<<-1<<endl; } else { //按照岛屿画圆与x轴的左交点排序 sort(points, points + num); temp=points[0]; for(int i=1;i<num;i++){ // if(points[i].left>temp.right)//temp.right为公共区间的右端点,若下一个区间的左端点大于temp.right则该区间与以上的公共区间没有公共部分 { rnum++;//雷达个数加1 temp=points[i];//更新公共区间右端点 }else if(points[i].right<temp.right){ temp=points[i];//更新公共区间右端点 } } cout<<"Case "<<casenum++<<": "<<rnum<<endl; } cin >> num >> rad; } return 0; }
相关文章推荐
- OC4_遵守多个协议
- 使用python开发app后台,xml和json的区别
- 关于类似Gmail邮件撤销功能实现初探
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- 看代码中遇到的难点
- intent传递字符串数组
- POJ 3107 Godfather (树的重心)
- iOS下载文件之NSData -UIImage
- 应用宝认领
- Excel Sheet Column Number -- leetcode
- json, protobuf-c++,pbc,sproto 性能测试与解决方案
- iphone使用开源代码Asyncsocket进行socket编程
- inno setup安装包程序完整版
- Android字符串及字符串资源的格式化
- sql server 日期范围
- NSFileManager和NSFileHandle
- Android Bundle类
- 统计多个wav文件的总音频时长
- 基本文件操作:NSFileHandle的用法
- ALV 中根据条件设置某个cell 是否可编辑