Watering Grass (贪心,最小覆盖)
参考:
https://blog.csdn.net/shuangde800/article/details/7828675
https://www.cnblogs.com/haoabcd2010/p/6171794.html?utm_source=itdadao&utm_medium=referral


#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; struct node
{
double L;
double R;
}a[]; bool cmp(node a, node b)
{
return a.R > b.R;
} void sort_node(int n)
{
for (int i=;i<n;i++)
{
int k=i;
for (int j=i+;j<n;j++)
{
if (a[j].R>a[k].R)
k=j;
}
swap(a[k],a[i]);
}
} int main()
{
int n;
double length, wide; // 都定义为double类型,因为使用勾股定理的时候肯定会算出小数
double center, radius; // center为圆心,radius为半径
double t;
while(scanf("%d%lf%lf", &n, &length, &wide) != EOF)
{
for(int i = ; i < n; ++i)
{
cin >> center >> radius;
t = sqrt(radius*radius - (wide/2.0)*(wide/2.0));
a[i].L = center - t;
a[i].R = center + t;
} //sort(a, a+n, cmp); 快排不知道为什么不能AC
sort_node(n); double border = ;
int cnt = ;
while(border < length) // 此类型题目的固定套路
{
int i;
for(i = ; i < n; ++i)
{
if(a[i].L <= border && a[i].R > border)
{
border = a[i].R;
cnt++;
break;
}
} if(i == n) // 如果i==n,说明遍历完了这n个区间,仍然没有找到符合条件的,即不能完全覆盖
break;
} if(border < length)
cout << - << endl;
else
cout << cnt << endl;
} return ;
}
Watering Grass (贪心,最小覆盖)的更多相关文章
- Watering Grass(贪心)
Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long and w meters ...
- UVA 10382 Watering Grass 贪心+区间覆盖问题
n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...
- UVa 10382 - Watering Grass 贪心,水题,爆int 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...
- 10382 - Watering Grass
Problem E Watering Grass Input: standard input Output: standard output Time Limit: 3 seconds n sprin ...
- Watering Grass(贪心算法)
给定一条草坪.草坪上有n个喷水装置.草坪长l米宽w米..n个装置都有每个装置的位置和喷水半径..要求出最少需要几个喷水装置才能喷满草坪..喷水装置都是装在草坪中间一条水平线上的. n sprinkle ...
- UVA 10382 Watering Grass(区间覆盖,贪心)题解
题意:有一块草坪,这块草坪长l 米,宽 w 米,草坪有一些喷头,每个喷头在横坐标为 p 处,每个喷头的纵坐标都是(w/2) ,并且喷头的洒水范围是一个以喷头为圆心,半径为 r 米的圆.每次最少需要打开 ...
- UVa 10382 Watering Grass (区间覆盖贪心问题+数学)
题意:有一块长为l,宽为w的草地,在其中心线有n个喷水装置,每个装置可喷出以p为中心以r为半径的圆, 选择尽量少的装置,把草地全部润湿. 析:我个去啊,做的真恶心,看起来很简单,实际上有n多个坑啊,首 ...
- UVA 10382 Watering Grass (区间覆盖,贪心)
问题可以转化为草坪的边界被完全覆盖.这样一个圆形就换成一条线段. 贪心,从中选尽量少的线段把区间覆盖,按照把线段按左端点排序,记录一个当前已经覆盖区间的位置cur, 从左端点小于等于cur选一个右端点 ...
随机推荐
- java基础之Integer包装类
Integer类概述: Integer 类在对象中包装了一个基本类型 int 的值 该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他 ...
- Django之模板语言(四) ------>Tags
案例1:单层for循环 # Django 模板语言测试代码 def template_test(request): name_list=["张三","李四",& ...
- Referenced assembly does not have a strong name
Step 1 : Run visual studio command prompt and go to directory where your DLL located. For Example my ...
- python冒泡排序算法的实现代码
python冒泡排序算法的实现代码 这篇文章主要介绍了python冒泡排序算法的实现代码,大家参考使用 1.算法描述: (1)共循环 n-1 次 (2)每次循环中,如果 前面的数大于后面的数,就交换 ...
- 组件:组合slot
<!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...
- Windows 常用工具 & 开发工具 & Chrome插件 & Firefox 插件 & 办公软件
常用工具 1.FastStone 8.0 2.印象笔记 3.Chrome 4.Beyond Compare 5.Everything 6.有道词典 7.文本编辑软件 EditPlus UltraEdi ...
- 常见问题:MongoDB基础知识
常见问题:MongoDB基础知识 ·MongoDB支持哪些平台? ·MongoDB作为托管服务提供吗? ·集合(collection)与表(table)有何不同? ·如何创建数据库(database) ...
- c++新特性实验(3)声明与定义:constexpr
1.作用 constexpr 声明一个函数或变量,它的值可以在编译时出现在常量表达式之中. 2.constexpr 变量要求 其类型必须是 字面类型 (LiteralType) . 它必须被立即初始化 ...
- Django项目:CRM(客户关系管理系统)--21--13PerfectCRM实现King_admin分页页数
{#table_data_list.html#} {## ————————08PerfectCRM实现King_admin显示注册表的字段表头————————#} {% extends 'king_m ...
- Thinkphp 数据库配置参数
mysql配置 'DB_USER' => array( 'DB_TYPE' => 'mysql', 'DB_HOST' => '127.0.0.1', 'DB_NAME' => ...