喷水装置(二)(最小线段覆盖)
2015-09-12 12:21
337 查看
题目地址
题目大意:有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水装置,每个喷水装置i喷水的效果是让以它为中心半径为Ri的圆都被润湿。请在给出的喷水装置中选择尽量少的喷水装置,把整个草坪全部润湿,题目给出每个装置的横坐标和半径。
解题思路:先将没用的装置(半径<h/2)去掉,再用贪心选择最合适的
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <stack>
using namespace std;
const int maxn = 1e4+100;
struct Circle
{
double l,r;
}cir[maxn];
bool cmp(Circle a,Circle b)
{
if(a.l == b.l) return a.r> b.r;
return a.l < b.l;
}
int main()
{
int t,n,cnt;
double x,y,w,h;
scanf("%d",&t);
while(t--)
{
scanf("%d%lf%lf",&n,&w,&h);
double minn = 0xffffff;
double maxx = 0;
cnt = 0;
for(int i = 0; i < n; i++)
{
scanf("%lf%lf",&x,&y);
if(y>=h/2)
{
cir[cnt].l = x-sqrt(y*y-h*h/4);
cir[cnt].r = x+sqrt(y*y-h*h/4);
minn = min(minn,cir[cnt].l);
maxx = max(maxx,cir[cnt].r);
cnt++;
}
}
if(minn > 0 || maxx < w)
{
puts("0");
continue;
}
sort(cir,cir+cnt,cmp);
double l=0,r=0;
int flag = 0;
maxx = 0;
int ans = 0;
while(r < w)
{
l = cir[flag].l;
r = cir[flag].r;
for(int i = 0; i < cnt; i++)
{
if(cir[i].l <= maxx && cir[i].r > r)
{
l = cir[i].l;
r = cir[i].r;
flag = i;
}
}
flag++;
maxx = max(maxx,r);
ans++;
}
printf("%d\n",ans);
}
return 0;
}
题目大意:有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水装置,每个喷水装置i喷水的效果是让以它为中心半径为Ri的圆都被润湿。请在给出的喷水装置中选择尽量少的喷水装置,把整个草坪全部润湿,题目给出每个装置的横坐标和半径。
解题思路:先将没用的装置(半径<h/2)去掉,再用贪心选择最合适的
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <stack>
using namespace std;
const int maxn = 1e4+100;
struct Circle
{
double l,r;
}cir[maxn];
bool cmp(Circle a,Circle b)
{
if(a.l == b.l) return a.r> b.r;
return a.l < b.l;
}
int main()
{
int t,n,cnt;
double x,y,w,h;
scanf("%d",&t);
while(t--)
{
scanf("%d%lf%lf",&n,&w,&h);
double minn = 0xffffff;
double maxx = 0;
cnt = 0;
for(int i = 0; i < n; i++)
{
scanf("%lf%lf",&x,&y);
if(y>=h/2)
{
cir[cnt].l = x-sqrt(y*y-h*h/4);
cir[cnt].r = x+sqrt(y*y-h*h/4);
minn = min(minn,cir[cnt].l);
maxx = max(maxx,cir[cnt].r);
cnt++;
}
}
if(minn > 0 || maxx < w)
{
puts("0");
continue;
}
sort(cir,cir+cnt,cmp);
double l=0,r=0;
int flag = 0;
maxx = 0;
int ans = 0;
while(r < w)
{
l = cir[flag].l;
r = cir[flag].r;
for(int i = 0; i < cnt; i++)
{
if(cir[i].l <= maxx && cir[i].r > r)
{
l = cir[i].l;
r = cir[i].r;
flag = i;
}
}
flag++;
maxx = max(maxx,r);
ans++;
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- Eclipse+超快速的模拟器Genymotion开展Android申请书(第一步:安装和配置Genymotion)
- SpringMVC+mybatis+spring整合教程
- struts2常用标签总结
- 【反思】一个价值两天的BUG,无论工作还是学习C语言的朋友都看看吧!
- php对表格进行批量操作如全选反选删除功能
- leetcode面试准备:Triangle
- [转载].NET Web开发技术(补充)
- iOS学习之KVC中setValuesForKeysWithDictionary:
- 网络请求之——————post
- HDU 1719 Friend 【规律】
- Android 百度地图开发(一)如何调用百度地图接口和在项目中显示百度地图以及实现定位
- Android基础入门教程——7.5.3 Android 4.4后WebView的一些注意事项
- 医疗数据库 Caché 开发笔记
- vs2012下编译protobuf
- Permutation Recovery(排列恢复)
- 为什么C++编译器不支持模板头文件和实现代码分离的编译
- Redis 的安装与使用(linux)
- android学习笔记(22)listview初步
- Mo2C-tag
- MYsql 数据库密码忘记(Window)