poj1328解题报告.
2015-09-14 10:57
393 查看
摘要:网上的算法都是贪心算法,但是很少有人说明一下为什么可以用贪心算法,现在来分析一下这个题目.首先本题是要找雷达的数量,可以很容易的想到雷达的可能位置由小岛确定,以小岛为圆心画圆就可以确定两个在x轴上的交点.
【1】雷达的位置占据了一个区间,这本质上就是一个活动安排问题.因为冲突的活动(区间)代表这只要把一个雷达放在相交的区间就可以满足小岛的覆盖).所以问题转化为寻找不相交雷达的最大集合的数量.
【2】有一个细节需要注意,当我们寻找下一个不相交区间时,要注意更新上一个区间(改变判断标准).
#include "stdafx.h" #include "iostream" #include<algorithm> #include "cmath" using namespace std; double bound1[1000],bound2[1000]; int coorx[1000], coory[1000]; bool compute(int x,int y,int d,double &result1,double &result2) { if((d*d- y*y < 0)||(y<0)) return false; else { result1 = x - sqrt(double(d*d) - double(y*y)); result2 = x + sqrt(double(d*d) - double(y*y)); return true; } } int Find(int n,int d) { double result1,result2; int count1 = 0,count2 = 0; int number = 0; return number; } void sort(int N)//插入排序,从大到小 { int i,j; for(i = 1;i<=N-1;i++) { double temp = bound1[i]; double temp2 = bound2[i]; for(j = i;j>0&&bound1[j-1]>temp;j--) { bound1[j] = bound1[j-1]; bound2[j] = bound2[j-1]; } bound1[j] = temp; bound2[j] = temp2; } } int main() { int n,d,count,count2 = 0; double result1,result2; bool check; while(1) { count2++; cin>>n; cin>>d; if(n==0&&d==0) break; count = n; check = true; memset(bound1,-100,sizeof(bound1)); memset(bound2,100,sizeof(bound2)); while(count--) { cin >> coorx[count]; cin >> coory[count]; if(compute(coorx[count],coory[count],d,result1,result2)) { bound1[count] = result1; bound2[count] = result2; } else check = false; } sort(n);//对雷达区间排序 int num = 0; double right = -1; for(int i=0;i<n;i++) { if(i==0) { num++; right = bound2[i]; } else { if(bound1[i] <= right)//有重叠 { if(bound2[i]<right) right = bound2[i]; continue; } else//没有重叠 { num++; right = bound2[i]; } } } if(d >= 0&&check==true) cout<<"Case "<<count2<<": "<< num<<endl; else cout<<"Case "<<count2<<": "<<-1<<endl; } return 0; }
相关文章推荐
- Java路径问题最终解决方案使用演示2
- 汤姆大叔深入理解JavaScript系列
- CSS3选择器(二)
- MYSQL Error Code: 1175 You are using safe update mode...解决方法
- gcc初谈(ubuntu下编译C语言)
- uploadify控制 上传图片到百度云存储
- 使用javascript实现在页面打印的效果的三种方式
- 《网络安全监控实战》
- 使用VMware 11 虚拟机安装Ubuntu 14
- Ruby对excel读写操作
- 数据库的四个范式
- AFNetworking实现程序重新启动时的断点续传
- 解决WPF程序中ListBox ItemsSource变化时不重置ScrollBar的问题
- C++类的定义之作用域
- jQuery 获取屏幕高度、宽度
- I2C 协议
- WEB项目转为Maven项目的两种方式
- spring源代码的批量操作
- gprof注意事项
- Android基础控件ToggleButton→ Switch→ SeekBar→ProgressBar 控件