NYOJ Radar 贪心之区间选点问题
来源:互联网 发布:淘宝主营类目多久更新 编辑:程序博客网 时间:2024/04/28 02:20
http://acm.nyist.net/JudgeOnline/status.php?pid=287
//贪的是:一个雷达能覆盖尽可能多的岛屿
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 1010
struct zb
{
double a,b;
}c[N];
int cmp(zb j,zb k)
{
return j.b<k.b;
}
int main()
{
int x,y,lap;
int n,r,i,t,d=0;
double p,q,tem1,tem2;
while(cin>>n>>r,n+r)
{
t=1; lap=0;
for(i=0;i<n;i++)
{
cin>>x>>y;
if(y>r)
lap=1;
p=x-sqrt(pow(r,2)-pow(y,2)); //将小岛坐标转化为x轴上的区间,进而把问题转化为区间选点问题
q=x+sqrt(pow(r,2)-pow(y,2));
c[i].a=p;
c[i].b=q;
}
if(lap==1)
{
cout<<"Case "<<++d<<": -1"<<endl;
continue;
}
sort(c,c+n,cmp);
//按坐标右端点进行升序排列
//若按左端点排列,除了判断当前坐标段左端点小于之前坐标段的最大右端点外,还要判断此坐标段的右端点与之前坐标的最大右端点那个较大,取较大的
//tem1=c[0].a;
tem2=c[0].b;
for(i=1;i<n;i++)
{
if(c[i].a<=tem2) //有重碟部分的直接跳过
//tem2=c[i].b; //不要按区间完全覆盖问题处理!!!
continue;
else
{
//tem1=c[i].a;
tem2=c[i].b;
t++;
}
}
cout<<"Case "<<++d<<": "<<t<<endl;
}
return 0;
}
//贪的是:一个雷达能覆盖尽可能多的岛屿
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 1010
struct zb
{
double a,b;
}c[N];
int cmp(zb j,zb k)
{
return j.b<k.b;
}
int main()
{
int x,y,lap;
int n,r,i,t,d=0;
double p,q,tem1,tem2;
while(cin>>n>>r,n+r)
{
t=1; lap=0;
for(i=0;i<n;i++)
{
cin>>x>>y;
if(y>r)
lap=1;
p=x-sqrt(pow(r,2)-pow(y,2)); //将小岛坐标转化为x轴上的区间,进而把问题转化为区间选点问题
q=x+sqrt(pow(r,2)-pow(y,2));
c[i].a=p;
c[i].b=q;
}
if(lap==1)
{
cout<<"Case "<<++d<<": -1"<<endl;
continue;
}
sort(c,c+n,cmp);
//按坐标右端点进行升序排列
//若按左端点排列,除了判断当前坐标段左端点小于之前坐标段的最大右端点外,还要判断此坐标段的右端点与之前坐标的最大右端点那个较大,取较大的
//tem1=c[0].a;
tem2=c[0].b;
for(i=1;i<n;i++)
{
if(c[i].a<=tem2) //有重碟部分的直接跳过
//tem2=c[i].b; //不要按区间完全覆盖问题处理!!!
continue;
else
{
//tem1=c[i].a;
tem2=c[i].b;
t++;
}
}
cout<<"Case "<<++d<<": "<<t<<endl;
}
return 0;
}
0 0
- NYOJ Radar 贪心之区间选点问题
- NYOJ 287 Radar 贪心之 区间选点
- POJ1328 Radar Installation (区间选点问题,贪心)
- nyoj 287 Radar 【区间选点】
- Radar Installation(贪心。 区间选点)
- [ACM] POJ 1328 Radar Installation (贪心,区间选点问题)
- poj 1328 Radar Installation 【贪心】【区间选点问题】
- POJ - 1328 Radar Installation (贪心,区间选点问题)
- POJ 1328 Radar Installation(贪心+区间选点问题)
- POJ 1328 Radar Installation (贪心,区间选点问题)
- (贪心,区间选点问题) Radar Installation POJ 1328
- nyoj 287 radar (区间选点)
- nyoj区间选点问题
- POJ1328 Radar Installation 【贪心·区间选点】
- POJ1328 Radar Installation 【贪心·区间选点】
- POJ-Radar Installation-贪心-区间选点
- POJ 1328 Radar Installation 贪心区间选点
- poj-1328 Radar Installation(贪心+区间选点)
- nginx-gridfs Benchmarking Raw Results
- Web Service实践之REST vs RPC
- 360doc.com无法复制破解方法
- 第十三周 阅读程序
- 设计模式-前言
- NYOJ Radar 贪心之区间选点问题
- __code__拿到代码信息
- 在vi中如何快速将文件移到行首和行尾?
- java基础(2)--接口与抽象类
- FreakZ学习笔记:路由发现机制
- cocos2d-x 3.0 场景切换特效汇总
- dict字典
- static关键字理解
- ScrollView嵌套GridView的解决办法