NYOJ 12:喷水装置(二)(贪心,区间覆盖问题)
12-喷水装置(二)
- 内存限制:64MB 时间限制:3000ms 特判: No
- 通过数:28 提交数:109 难度: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
思路
因为所有的喷水装置都在横向中心线上,所以可以利用圆与草坪边界的交点来转换成区间覆盖问题
区间覆盖问题的详解:https://blog.csdn.net/chenguolinblog/article/details/7882316
AC代码
/*
* @Author: WZY
* @School: HPU
* @Date: 2018-10-20 15:19:19
* @Last Modified by: WZY
* @Last Modified time: 2018-10-20 17:09:48
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#include <time.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
#define bug cout<<"---------"<<endl
#define debug(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<"\n"
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
struct wzy
{
double l,r;
}p[maxn];
double x[maxn],r[maxn];
bool cmp(wzy u,wzy v)
{
if(u.l==v.l)
return u.r>v.r;
return u.l<v.l;
}
inline double MAX(double a,double b)
{
return a>b?a:b;
}
inline double MIN(double a,double b)
{
return a<b?a:b;
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
double _begin_time = clock();
#endif
int t;
int n,w,h;
cin>>t;
while(t--)
{
cin>>n>>w>>h;
double res=h/2.0;
double _;
for(int i=0;i<n;i++)
{
cin>>x[i]>>r[i];
if(r[i]<=res)
_=0.0;
else
_=sqrt(r[i]*r[i]*1.0-res*res*1.0);
p[i].l=x[i]-_;
p[i].r=x[i]+_;
}
sort(p,p+n,cmp);
int ans=0;
double vis=0.0;
double __;
for(int i=0;i<n;i++)
{
if(p[i].l<=vis)
{
__=p[i].r;
while(p[i].l<=vis)
{
__=MAX(p[i].r,__);
i++;
if(i==n)
break;
}
vis=__;
i--;
ans++;
}
if(vis>=w)
break;
}
if(vis>=w)
cout<<ans<<endl;
else
cout<<0<<endl;
}
#ifndef ONLINE_JUDGE
double _end_time = clock();
printf("time = %lf ms.", _end_time - _begin_time);
#endif
return 0;
}
NYOJ 12:喷水装置(二)(贪心,区间覆盖问题)的更多相关文章
- nyoj 12——喷水装置二——————【贪心-区间覆盖】
喷水装置(二) 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的 ...
- 高效算法——E - 贪心-- 区间覆盖
E - 贪心-- 区间覆盖 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/E 解题思路: 贪心思想, ...
- 【题解】Cut the Sequence(贪心区间覆盖)
[题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...
- 基于贪心算法的几类区间覆盖问题 nyoj 12喷水装置(二) nyoj 14会场安排问题
1)区间完全覆盖问题 问题描述:给定一个长度为m的区间,再给出n条线段的起点和终点(注意这里是闭区间),求最少使用多少条线段可以将整个区间完全覆盖 样例: 区间长度8,可选的覆盖线段[2,6],[1, ...
- NYOJ 12 喷水装置(二)
pid=12">喷水装置(二) 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描写叙述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n( ...
- 南阳OJ-12-喷水装置(二)贪心+区间覆盖
题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=12 题目大意: 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有 ...
- UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...
- 51nod 1091 线段的重叠【贪心/区间覆盖类】
1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...
- UVA 10382 Watering Grass 贪心+区间覆盖问题
n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...
- B. Heaters 思维题 贪心 区间覆盖
B. Heaters 这个题目虽然只有1500的分数,但是我还是感觉挺思维的,我今天没有写出来,然后看了一下题解 很少做这种区间覆盖的题目,也不是很擅长,接下来讲讲我看完题解后的思路. 题目大意是:给 ...
随机推荐
- PHP -S命令 PHP内置web服务器
手册详细介绍 : http://www.php.net/manual/zh/features.commandline.webserver.php 适合本地开发 php 5.4.0起 这个内置的Web ...
- linux下iostat命令详解
iostat用于输出CPU和磁盘I/O相关的统计信息 iostat语法 用法:iostat [ 选项 ] [ <时间间隔> [ <次数> ]] 常用选项说明: -c:只显示系统 ...
- AdminLTE 文档
一个基于 bootstrap 的轻量级后台模板,这个前端界面个人感觉很清爽,对于一个大后端的我来说,可以减少较多的时间去承担前端的工作但又必须去独立去完成一个后台系统开发的任务,并且,文档还算比较齐全 ...
- nginx+tomcat集群
参考: 简单:http://blog.csdn.net/wang379275614/article/details/47778201 详细:http://www.jb51.net/article/77 ...
- node(1) npm是什么?node的异步概念
NPM是随同的NodeJS一起安装的包管理工具 他可以做什么? 1.可以从NPM服务器下载别人的东西使用 2.可以把自己的东西传到NPM服务器,让别人下载使用 淘宝的镜像会快一点 cnpm ...
- C语言进度条实现。(转)
#include <stdio.h> #include <windows.h> //跟新进度条函数 /* * 每传入一个参数就刷新一次进度条 * 当i*<=percent ...
- Java中泛型Class<T>、T与Class<?>
一.区别 单独的T 代表一个类型 ,而 Class<T>代表这个类型所对应的类, Class<?>表示类型不确定的类 E - Element (在集合中使用,因为集合中存放的是 ...
- scanf *的用法
scanf *的用法 char a[LEN]="12:13:14"; char i,j,k,h,l,m; sscanf(a,"%*c%*c:% ...
- redis 五大数据类型之list篇
1.lpush/lpop/rpop/rpush/lrange 这里的list就是链表,lpush就是从左往右插入数据,rpush就是从右往左插数据,lrange就是从左往右范围性的取值,pop就是弹 ...
- WebClient和WebRequest获取html代码
HTML: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.as ...