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的分数,但是我还是感觉挺思维的,我今天没有写出来,然后看了一下题解 很少做这种区间覆盖的题目,也不是很擅长,接下来讲讲我看完题解后的思路. 题目大意是:给 ...
随机推荐
- POJ 1035 Spell checker 字符串 难度:0
题目 http://poj.org/problem?id=1035 题意 字典匹配,单词表共有1e4个单词,单词长度小于15,需要对最多50个单词进行匹配.在匹配时,如果直接匹配可以找到待匹配串,则直 ...
- Linux十字病毒查杀处理
之前处理过一次十字病毒,但未好好整理处理过程,现在转载一篇来自51cto的文章. 转自:http://blog.51cto.com/ixdba/2163018 十字符病毒,杀不死的小强,一次云服务器沦 ...
- C++标准异常概述
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- 【转载】非对称加密过程详解(基于RSA非对称加密算法实现)
1.非对称加密过程: 假如现实世界中存在A和B进行通讯,为了实现在非安全的通讯通道上实现信息的保密性.完整性.可用性(即信息安全的三个性质),A和B约定使用非对称加密通道进行通讯,具体 ...
- C++ STL string对象操作汇总
string对象 C语言只提供了一个char类型用来处理字符,而对于字符串,只能通过字符串数组来处理,显得十分不便.C++STL提供了string基本字符系列容器来处理字符串,可以把string理解为 ...
- getchar getche getch的区别
getchar 由宏实现:#define getchar() getc(stdin). getchar有一个int型的返回值.当程序调用getchar时.程序就等着用户按键.用户输入的字符被存放在键盘 ...
- Cracking The Coding Interview 5.5
#include <iostream> #include <vector> using namespace std; int getNum1(int N) { int num= ...
- minidebug学习分析 01 基本框架
0x01 基本框架 基本框架就是CreateProcess启动目标程序,再通过调试事件DEBUG_EVENT在调试循环中监控程序的行为. (1)CreatProcess BOOL CreateP ...
- Ubuntu 14.04 安装 Xilinx ISE 14.7 全过程(转)
reference:https://www.cnblogs.com/tracyone/p/3561781.html 按照ISE后,建立ISE启动连接: 这个帖子作为我安装xilinx ISE 14.7 ...
- BSTR 转 char*
#include <comdef.h> #include <comutil.h> #pragma comment(lib,"comsuppw.lib") _ ...