NYOJ 12 喷水装置(二)(贪心)
2016-12-08 20:31
393 查看
喷水装置(二)
时间限制:3000 ms | 内存限制:65535 KB难度:4
描述有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水装置,每个喷水装置i喷水的效果是让以它为中心半径为Ri的圆都被润湿。请在给出的喷水装置中选择尽量少的喷水装置,把整个草坪全部润湿。
输入第一行输入一个正整数N表示共有n次测试数据。
每一组测试数据的第一行有三个整数n,w,h,n表示共有n个喷水装置,w表示草坪的横向长度,h表示草坪的纵向长度。
随后的n行,都有两个整数xi和ri,xi表示第i个喷水装置的的横坐标(最左边为0),ri表示该喷水装置能覆盖的圆的半径。
输出每组测试数据输出一个正整数,表示共需要多少个喷水装置,每个输出单独占一行。
如果不存在一种能够把整个草坪湿润的方案,请输出0。
样例输入
2 2 8 6 1 1 4 5 2 10 6 4 5 6 5
样例输出
1 2
来源《算法艺术与信息学竞赛》
上传者
张云聪
基本的思想是:从左边开始,每次找一个能够向右边延伸最多的装置,然后跳到那个装置上重复上述操作。这题要注意很多细节问题。
代码:
#include <iostream>
#include <queue>
#include <string>
#include <stack>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
int n,w,h;
int x[10005];
int r[10005];
struct note
{
double l,r;
};
typedef struct note note;
note a[10005];
int cmp(note a,note b)
{
return a.l<b.l;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&w,&h);
int i;
for(i=0;i<n;i++)
{
scanf("%d%d",&x[i],&r[i]);
}
for(i=0;i<n;i++)
{
double tmp;
if(r[i]<=h*1.0/2)
{
tmp=0;
}
else
{
tmp=sqrt(r[i]*r[i]-h*h/4);
}
a[i].l=x[i]-tmp;
a[i].r=x[i]+tmp;
}
sort(a,a+n,cmp);
if(a[0].l>0)
{
printf("0\n");
continue;
}
int index=0;
int maxx=-1;
for(i=0;i<n;i++)
{
if(a[i].l>0) break;
if(a[i].r>maxx)
{
index=i;
maxx=a[i].r;
}
}
i=index;
int f=0;
int cnt=0;
while(i<n)
{
cnt++;
int j=i;
int flag=i;
maxx=-1;
while(a[j].l<=a[i].r && j<n)
{
if(a[j].r>maxx)
{
maxx=a[j].r;
flag=j;
}
j++;
}
if(a[i].r>=w)
{
break;
}
if(flag==i)
{
f=1;
break;
}
i=flag;
}
if(f)
{
printf("0\n");
continue;
}
printf("%d\n",cnt);
}
return 0;
}
相关文章推荐
- NYOJ12 喷水装置(二)【贪心】
- nyoj 12 喷水装置(二)【贪心】+【区间完全覆盖覆盖】
- NYOJ 12 喷水装置(二)( 贪心)
- NYOJ 12 喷水装置(二)【贪心+区间覆盖】
- NYOJ 12-喷水装置(二)(简单贪心)
- nyoj--12--喷水装置(二)(区间覆盖问题+贪心)
- nyoj--12--喷水装置(二)(区间覆盖问题+贪心)
- NYOJ-----12---喷水装置(二)贪心
- NYOJ 12 喷水装置(2)贪心之区间覆盖问题
- 贪心算法之——喷水装置二(nyoj12)
- nyoj12 喷水装置(二)贪心
- nyoj 12 喷水装置(二)(贪心)
- nyoj12-喷水装置(二)(贪心)
- nyoj 12 喷水装置(二) 贪心 区间覆盖
- 贪心nyoj12 喷水装置(二)
- NYOJ-12 喷水装置2(贪心 区间覆盖)
- NYOJ12 喷水装置(二)【贪心】
- NYOJ 12 喷水装置(二)
- nyoj 12 喷水装置二
- 贪心——(区间覆盖问题)——ny_6喷水装置(一)ny_12喷水装置(二)