UVa 10382 - Watering Grass(贪心算法,区间覆盖问题)

别再死记硬背GNN公式了!用PyTorch Geometric从零实现一个GraphSAGE(附完整代码) 本文通过PyTorch Geometric从零实现GraphSAGE模型,深入解析图神经网络的核心原理与实战应用。文章提供完整代码示例,涵盖环境配置、邻居采样、信息聚合等关键步骤,帮助开发者掌握图表示学习技术,避免陷入公式推导的困境。适合希望将GNN应用于实际项目的中级开发者和学生。 阅读详情

Problem E
Watering Grass
Input:
standard input
Output: standard output
Time Limit: 3 seconds

n sprinklers are installed in a horizontal strip of grass l meters long andw meters wide. Each sprinkler is installed at the horizontal center line of the strip. For each sprinkler we are given its position as the distance from the left end of the center line and its radius of operation.

What is the minimum number of sprinklers to turn on in order to water the entire strip of grass?

Input

Input consists of a number of cases. The first line for each case contains integer numbersn, l and w with n <= 10000. The next n lines contain two integers giving the position of a sprinkler and its radius of operation. (The picture above illustrates the first case from the sample input.)

 

Output

For each test case output the minimum number of sprinklers needed to water the entire strip of grass. If it is impossible to water the entire strip output -1.

Sample input

8 20 2

5 3

4 1

1 2

7 2

10 2

13 3

16 2

19 4

3 10 1

3 5

9 3

6 1

3 10 1

5 3

1 1

9 1

 

Sample Output

6

2

-1


(Regionals 2002 Warm-up Contest, Problem setter: Piotr Rudnicku)

 

思路:

贪心思想,将问题转化为区间覆盖问题,将草地的上边界作为要覆盖的区间,计算出每个洒水器覆盖的区间范围,不能覆盖的舍去,然后将洒水器按覆盖范围的左边界升序排列。

要覆盖的最右边的点rightmost的初始值为0,遍历洒水器,找一个能覆盖住rightmost且覆盖范围的右边界最大的洒水器,然后将该洒水器覆盖的右边界作为新的rightmost,重复刚才的过程,直到覆盖整个草地。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;

#define MAX_SIZE 10000

struct Sprinkler
{
	double left;
	double right;
	bool operator < (const Sprinkler &s) const
	{
		return left < s.left;
	}
}sprinklers[MAX_SIZE+5];

int WaterTheGrass(int m, int l)
{
	double rightmost = 0.0;
	int count = 0;
	int i, j;
	for (i = 0; i < m; i = j)
	{
		if (sprinklers[i].left > rightmost) break;
		for (j = i+1; j < m && sprinklers[j].left <= rightmost; ++j)
		{
			if (sprinklers[j].right > sprinklers[i].right)
			{
				i = j;
			}
		}
		++count;
		rightmost = sprinklers[i].right;
		if (rightmost >= l) break;
	}
	if (rightmost >= l)
	{
		return count;
	}
	return -1;
}

int main(void)
{
	int n, l;
	double w;
	while (cin >> n >> l >> w)
	{
		w /= 2.0;
		int i, m = 0;
		for (i = 0; i < n; ++i)
		{
			int p, r;
			scanf("%d%d", &p, &r);
			if (r > w)
			{
				double halfCoveredLen = sqrt((double)r*r - w*w);	//注意转化为double型,错了好几次...
				sprinklers[m].left = (double)p - halfCoveredLen;
				sprinklers[m++].right = (double)p + halfCoveredLen;
			}
		}
		sort(sprinklers, sprinklers+m);
		cout << WaterTheGrass(m, l) << endl;
	}
	return 0;
}
欢迎转载,请尊重作者,转载请标明出处: http://blog.youkuaiyun.com/code_pang/article/details/17919533

使用LoRa远程控制路灯(PWM调光以及LoRaWAN单播、组播、广播) PWM调光 1、使用有pwm波输出的引脚,本例子使用timer0,PA0引脚; 2、由于timer时钟的原因,mcu不能进入休眠; 3、主控使用EFM32,代码实现如下: #define MAX_PULSE (CPU_FREQ/1000-1) #define UNIT_PULSE MAX_PULSE/100 //单位脉宽 void pwminit(void) { ... 阅读详情

相关推荐

【SAP】ABAP——ALV显示后选择数据操作

在ALV显示后,可全选、全不选或者选择几条数据进行发邮件、更新、下单等等操作。 在user_command中的实现方式如下: 方式一: FORM user_command USING ucomm LIKE sy-ucomm selfield TYPE slis_selfield. DATA: lr_grid TYPE REF TO cl_gui_alv_grid. DATA: lt_rows TYPE lvc_t_row WITH

Mona_1220的博客 4091

5201. 给植物浇水

你打算用一个水罐给花园里的n株植物浇水。植物排成一行,从左到右进行标记,编号从0到n - 1。其中,第i株植物的位置是x = i。x = -1处有一条河,你可以在那里重新灌满你的水罐。 每一株植物都需要浇特定量的水。你将会按下面描述的方式完成浇水: 按从左到右的顺序给植物浇水。 在给当前植物浇完水之后,如果你没有足够的水完全浇灌下一株植物,那么你就需要返回河边重新装满水罐。 你不能提前重新灌满水罐。 最初,你在河边(也就是,x = -1),在 x 轴上每移动一个单...

stay hungry ! stay foolish! 686

数字逻辑设计大程——以撒的结合(Verilog语言)

前言 在大二学习数字逻辑设计课程(即计算机组成课程的前引课)时,期末大程题目我和同组的github成员lwaekfjlk 决定写一款基于Verilog语言的游戏。原型是我们两人都非常喜爱的一款游戏——The Binding of Isaac: Rebirth。在三天三夜的奋战后,基本完成了预期目标。由于亲自编写了其中种种,我们深知新手初期使用Verilog语言有多困难。而可悲的是,这又是之后计算机组成课程和体系结构课程所需的!因此,我们将top部分源码放在这里,供同袍们参考。若想引用还请注明出处。 代码部分

LuvLucifer的博客 1383

Watering Grass(区间覆盖+贪心)

n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each sprinkler is installed at the horizontal center line of the strip. For each sprinkler we are given its position as the distance from the left end of the center

qq_62545570的博客 352

FOC控制必看:单/双/三电阻电流采样方案全对比(附NXP实战避坑指南)

本文深入对比了FOC控制中单电阻、双电阻和三电阻电流采样方案的优劣与适用场景,重点剖析了采样时序、窗口与ADC转换的工程挑战。针对NXP平台,提供了解决采样窗口过窄的实战方案与配置指南,帮助工程师根据成本与性能需求做出最优选择,并规避常见设计陷阱。

n2o3p4的博客 412

【Codesys】获取从站信息,判断是否从站断联,总线丢失

获取EtherCAT从站信息,判断是否从站断联,总线丢失

qq_35316470的博客 5731

UVA - 10382 Watering Grass(降维处理,贪心不停)

题目链接:https://cn.vjudge.net/contest/311860#problem/C Sample Input 8 20 2 5 3 4 1 1 2 7 2 10 2 13 3 16 2 19 4 3 10 1 3 5 9 3 6 1 3 10 1 5 3 1 1 9 1 Sample Output 6 2 -1 解析:刚开始输入三个数,8 20 2。8个圆,矩形的长宽分别...

zaiyang遇见 271

微信小程序 - 实现仿小红书轮播图组件功能,类似小红书笔记页面图片展示播放效果,微信小程序仿小红书幻灯片轮播支持视频播放、图片预览,自动适应图片或视频大小可左右滑动切换(小程序精美轮播图,丝滑切换)

微信小程序,轮播图组件,微信小程序仿小红书轮播图,小红书轮播图仿造,微信小程序轮播图,微信小程序幻灯片,微信小程序高仿小红书轮播图实现,小程序实现类似小红书一样的轮播图组件,轮播图插件,精致轮播图,精美丝滑流畅切换的轮播图,轮播图组件,小红薯轮播图,关于小红书的小程序不同数量的轮播图,展示不一样的轮播指示点的实现,微信小程序仿小红书图片轮播,开发微信小程序模仿小红书,高仿小红书Banner的指示器,小红书小程序笔记详情轮播图指示点的随图片数量的变化动态变化,微信小程序swiper轮播图,js轮播图,html

✅ 精选推荐文章 6929

Watering Grass(贪心 区间覆盖)

Watering Grass(贪心 区间覆盖) n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each sprinkler is installed at the horizontal center line of the strip. For each sprinkler we are given its position as the distance from th

Anthcony的博客 255

新手必看!一步步教您如何获取可用的D音Cookie(为什么我复制的D音Cookie无法正常使用?)

新手必看!一步步教您如何获取可用的抖音Cookie(为什么我复制的抖音Cookie无法正常使用,无法下载无水印视频?)

XiaoqiangClub的博客 1491

华为Ascend_toolkit在x86ubuntu安装失败报错。[WARNING] driver package is not installed. If you install in docker

这个报错发生在交叉编译配置Ascend_toolkit环境时,安装Ascend_toolkit安装包的时候发生。[Y/N]Y。

anananajiushiwo的博客 1018

uva10382(贪心算法---区间覆盖问题)

https://vjudge.net/problem/UVA-10382(uva10382 点击打开题目链接)

Acmer_future_victor的博客 709

2023 ICRA《Visual Language Maps for Robot Navigation》阅读笔记

视觉语言导航可以使用预先训练的现有的视觉语言模型来将图像与对象目标的自然语言描述相匹配,但它与绘制环境的过程仍然脱节,缺乏经典几何地图的空间精度。为了解决这个问题,我们提出了VLMaps,这是一种空间地图表示,它将预训练的视觉语言特征与物理世界的3D重建直接融合在一起。VLMaps可以使用标准探索方法从机器人上的视频馈送中自主构建,并在没有额外标记数据的情况下实现地图的自然语言索引。具体来说,当与大型语言模型(LLM)相结合时,VLMaps可以用于。

qq_44100524的博客 1990

UVa-10382 Watering Grass **

/* * Uva-10382-Watering Grass.cpp *  特别注意精度。。(感觉这道题的判题有问题,开始怎么交都WA,,过两天什么都没改,再交就AC了 , 汗。。 * * 详细代码注释: http://apps.hi.baidu.com/share/detail/24628400 * * Created on: 2011-10-3 * Author: */#inc...

weixin_34399060的博客 98

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

UVa 10382 - Watering Grass(区间覆盖问题)

UVa 10382 - Watering Grass(区间覆盖问题) n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each sprinkler is installed at the horizontal center line of the strip. Fo...

weixin_43179892的博客 273

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

UVA 10382 Watering Grass

https://cn.vjudge.net/problem/UVA-10382 https://uva.onlinejudge.org/external/103/10382.pdf 题目 草地上有许多喷水的水龙头,知道半径和位置,需要完全覆盖草地,选出最少的水龙头。输出最少的数量。 题解 画出三角形,变成区间覆盖问题 尽量选右端点靠右的区间(前提是包含当前的点),然后把当...

234

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

UVA - 10382 Watering Grass(贪心)

贪心 区间覆盖

riversu 432

Watering Grass UVA - 10382 贪心

问题 https://vjudge.net/problem/UVA-10382 分析 将一个点的覆盖范围看作是一个长方形,舍弃弓形区域,变成区间覆盖问题,用贪心法 注意:bb-ww/4有可能小于0,要筛出 #include <iostream> #include <cstdio> #include <cmath> #include <cstring>...

zpf1998的博客 173

UVA 10382 Watering Grass 区间覆盖问题 -

题目地址:http://vjudge.net/problem/UVA-10382 第一想法: 1)ri*2 2)肯定为了让决策有序化,按p排序 3)很明显 ,状态两种:开与不开,而且位置固定的,那么就可以看成由n个区间,求能覆盖整个区间的最小数量,那不是就贪心的区间覆盖问题嘛

BRCOCOLI的博客 361

UVa 10382 - Watering Grass

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1323 原题: n sprinklers are installed in a horizontal strip of grass l meters l

D_Double's Journey 4592

Watering Grass

UVA 10382 Watering Grass (贪心 + 区间覆盖问题) **n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each sprinkler is installed at the horizontal center line of the str...

我想要西瓜 422

UVA - 10382 Watering Grass区间完全覆盖问题

n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each sprinkler is installed at the horizontal center line of the strip. For each sprinkler we are given its p...

DouglasConnor的博客 242

UVA10382 Watering Grass 贪心

题意是用最少的圆覆盖一个矩形区域,其实一个园相当于一个区间。然后就是用最少的小区间覆盖大区间的问题。贪心搞之。。。。 在一个小问题上纠结了好久,不应该。居然直接拿x-r了。。高中几何全部丢给老师了   Watering Grass Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

774

uva 10382 - Watering Grass(贪心)

题目链接http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1323 Problem E Watering Grass Input: standard input Output: standard output Time Limit: 3 second

勿忘初衷 1022

UVa 10382 - Watering Grass(贪心算法)

UVa的题目地址如下: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1323

木飞骄的博客 691

UVA10382 - Watering Grass 题解

原题链接 Online Judge: 10382 - Watering Grass 洛谷:UVA10382 Watering Grass LibreOJ:#10002. 「一本通 1.1 例 3」喷水装置,题目稍有不同,数据组数确定为 TTT。 解决题目 先通过勾股定理求出每个圆向左向右实际能覆盖的距离 kkk,将题目中的圆转化为矩形。 可以看出来这是一道贪心问题。我这里使用的方法是从右往左覆盖草坪,并用 lastlastlast 标记当前覆盖到的位置。 每次循环中从左向右枚举右侧(ededed)能到达

digitalWrite的博客 234
上一篇: UVa 10905 - Children's Game(求多个正整数排列后,所得的新的数字的极值)
下一篇: UVa 11549 - Calculator Conundrum(计算循环节)
庞老板
博客等级 码龄15年 127粉丝 66原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

庞老板

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值