poj1328贪心
来源:互联网 发布:取色笔 淘宝 编辑:程序博客网 时间:2024/05/16 19:23
本题难点在于思维的转化,应该通过海岛确定雷达的范围,再去重,而不是确定雷达的坐标看能覆盖几个岛。
先对岛按x坐标排序,第一个雷达放在第一个海岛对应的区间的右端点,对后面的雷达坐标进行贪心,如果当前区间的左端点在上一个雷达的右边,则应放置一个新雷达在当前区间的右端点,如果当前区间的右端点在上个雷达左边则应把上一个雷达移到当前区间的右端点(思维难点,画图易懂,这时雷达如果不动的话无法覆盖当前区间对应的海岛)。
<pre name="code" class="cpp">#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <math.h> using namespace std;struct node{double x,y;}site[1001];int cmp(node a,node b){return a.x<b.x;}int main(){double R;int n;int cnt=0;while(scanf("%d %lf",&n,&R)&&n&&R){int flag=0;cnt++;int ans=1;for(int i=0;i<n;i++){scanf("%lf %lf",&site[i].x,&site[i].y);if(site[i].y>R){flag=1;}}if(flag){printf("Case %d: -1\n",cnt);continue;}sort(site,site+n,cmp);double l[1001],r[1001];for(int i=0;i<n;i++){l[i]=site[i].x-sqrt(R*R-site[i].y*site[i].y);r[i]=site[i].x+sqrt(R*R-site[i].y*site[i].y);}for(int i=0, temp=r[0];i<n-1;i++){if(l[i+1]>temp){temp=r[i+1];ans++;}else if(r[i+1]<temp)temp=r[i+1];}printf("Case %d: %d\n",cnt,ans);}return 0;}
0 0
- POJ1328贪心
- POJ1328--贪心
- poj1328(贪心)
- poj1328 贪心
- poj1328 贪心
- poj1328 贪心
- poj1328贪心
- POJ1328(贪心)
- poj1328贪心
- poj1328 贪心
- POJ1328 贪心
- poj1328贪心
- poj1328-贪心
- POJ1328 贪心
- 贪心poj1328
- POJ1328 Radar Installation 贪心
- 简单的贪心 poj1328
- 贪心算法 | Poj1328
- (超快速计算素数个数)10的11次方内素数个数
- 浅谈#ifndef~#define~#endif~的作用
- Activity Lifecycle
- Layout布局之线性布局
- Excel公式教程 数组公式基础:数组扩展
- poj1328贪心
- 用Win7自带的工具给硬盘分区与合并
- Redis之——搭建高可用及负载均衡的Redis
- 19. Remove Nth Node From End of List(python)
- git ssh 心得
- shell中的xargs命令
- 初入微电商,我为何选择电脑护目镜
- LFM推荐的例子和代码
- TextView