Dividing the Path(POJ 灌溉草场) 动态规划 优先队列

2024-01-31 06:18

本文主要是介绍Dividing the Path(POJ 灌溉草场) 动态规划 优先队列,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Dividing the Path点击转到

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5588 Accepted: 1968

Description

Farmer John's cows have discovered that the clover growing along the ridge of the hill in his field is particularly good. To keep the clover watered, Farmer John is installing water sprinklers along the ridge of the hill. 

To make installation easier, each sprinkler head must be installed along the ridge of the hill (which we can think of as a one-dimensional number line of length L (1 <= L <= 1,000,000); L is even). 

Each sprinkler waters the ground along the ridge for some distance in both directions. Each spray radius is an integer in the range A..B (1 <= A <= B <= 1000). Farmer John needs to water the entire ridge in a manner that covers each location on the ridge by exactly one sprinkler head. Furthermore, FJ will not water past the end of the ridge in either direction. 

Each of Farmer John's N (1 <= N <= 1000) cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval (S,E). Each of the cow's preferred ranges must be watered by a single sprinkler, which might or might not spray beyond the given range. 

Find the minimum number of sprinklers required to water the entire ridge without overlap. 

Input

* Line 1: Two space-separated integers: N and L 

* Line 2: Two space-separated integers: A and B 

* Lines 3..N+2: Each line contains two integers, S and E (0 <= S < E <= L) specifying the start end location respectively of a range preferred by some cow. Locations are given as distance from the start of the ridge and so are in the range 0..L.

Output

* Line 1: The minimum number of sprinklers required. If it is not possible to design a sprinkler head configuration for Farmer John, output -1.

Sample Input

2 8
1 2
6 7
3 6

Sample Output

3

Hint

INPUT DETAILS: 

Two cows along a ridge of length 8. Sprinkler heads are available in integer spray radii in the range 1..2 (i.e., 1 or 2). One cow likes the range 3-6, and the other likes the range 6-7. 

OUTPUT DETAILS: 

Three sprinklers are required: one at 1 with spray distance 1, and one at 4 with spray distance 2, and one at 7 with spray distance 1. The second sprinkler waters all the clover of the range like by the second cow (3-6). The last sprinkler waters all the clover of the range liked by the first cow (6-7). Here's a diagram: 

                 |-----c2----|-c1|       cows' preferred ranges|---1---|-------2-------|---3---|   sprinklers+---+---+---+---+---+---+---+---+0   1   2   3   4   5   6   7   8


The sprinklers are not considered to be overlapping at 2 and 6.

Source

USACO 2004 December Gold

1.题目含义:

      在一片草场上:有一条长度为L (1 <= L <= 1,000,000,L为偶数)的线 段。 John的N (1 <= N <= 1000) 头奶牛都沿着草场上这条线段吃草,每头 牛的活动范围是一个开区间(S,E),S,E都是整数。不同奶牛的活动范围可以 有重叠。John要在这条线段上安装喷水头灌溉草场。每个喷水头的喷洒半径可以随 意调节,调节范围是 [A,B ](1 <= A <= B <= 1000),A,B都是整数。要求线段上的每个整点恰好位于一个喷水头的喷洒范围内 每头奶牛的活动范围要位于一个喷水头的喷洒范围内 任何喷水头的喷洒范围不可越过线段的两端(左端是0,右端是L )   请问, John 最少需要安装多少个喷水头。

2.解题思路:

 2.1  X满足:

        2.1.1X为偶数(因为X为奇数,将不能灌溉整个L区域)
     2.1.2X所在位置不会出现奶牛,即X不属于任何一个(S,E)
     2.1.3 X大于等于2A
     2.1.4 当X大于2B时,存在Y属于 [X-2B X-2A]   且Y满足上述三个条件,使得f(X)=f(Y)+1

  2.2  递推计算f(X)
f(X) = ∝ : X 是奇数
f(X) = ∝ : X < 2A
f(X) = ∝ :X处可能有奶牛出没
f(X)=1: 2AX2B 、且X位于任何奶牛的活动范围之外
f(X)=1+min{f(Y): Y[X-2B X-2A]、Y位于任何奶牛的活动范围之外}: X>2B

3.本题可以学习,牛活动区域的存储问题。(区间标记)

#include<iostream>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cmath>
#include<cstdlib>
using namespace std;
#define maxl 1000010
#define maxn 1010
#define inf 0x3f3f3f3f
int f[maxl];
int cow[maxl];
int n,l,a,b;
struct ans
{int x,f;//f是喷水头的个数 bool operator<(const ans &a)const{return f>a.f;//从小到大排序 }ans(int xx=0,int yy=0):x(xx),f(yy){}
};
priority_queue<ans> q;
int main()
{cin>>n>>l;cin>>a>>b;while(!q.empty()){q.pop();}a=a*2;//变为直径 b=b*2;int s,e;memset(cow,0,sizeof(cow));for(int i=0;i<n;i++){cin>>s>>e;++cow[s+1];//从s+1处起,进入一个奶牛活动的区域 --cow[e];//一个奶牛活动区域,结束的边界 }int nowcows=0;//表示当前节点,位于多少头奶牛的活动范围之内 for(int i=0;i<=l;i++)//记录每个节点,是否有奶牛 {f[i]=inf;nowcows=nowcows+cow[i];cow[i]=nowcows;}//for(int i=0;i<=l;i++) //cout<<cow[i]<<"*****"<<i<<endl;for(int i=a;i<=b;i+=2)//队列初始化 {if(!cow[i]) {f[i]=1;if(i<=b+2-a){q.push(ans(i,1));}}}for(int i=b+2;i<=l;i+=2)//y位于[X-2*B,X-2*A]范围内的时候 {if(!cow[i])//位于奶牛的活动范围之外,且在[X-2*B,X-2*A]之间 {ans x;while(!q.empty()){x=q.top();if(x.x<i-b) {q.pop();}else break;}if(!q.empty())f[i]=x.f+1;}if(f[i-a+2]!=inf){q.push(ans(i-a+2,f[i-a+2]));}}if(f[l]==inf)  cout<<-1<<endl;else cout<<f[l]<<endl;return 0;
}

 

这篇关于Dividing the Path(POJ 灌溉草场) 动态规划 优先队列的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/662810

相关文章

Java中常见队列举例详解(非线程安全)

《Java中常见队列举例详解(非线程安全)》队列用于模拟队列这种数据结构,队列通常是指先进先出的容器,:本文主要介绍Java中常见队列(非线程安全)的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录一.队列定义 二.常见接口 三.常见实现类3.1 ArrayDeque3.1.1 实现原理3.1.2

Java调用C#动态库的三种方法详解

《Java调用C#动态库的三种方法详解》在这个多语言编程的时代,Java和C#就像两位才华横溢的舞者,各自在不同的舞台上展现着独特的魅力,然而,当它们携手合作时,又会碰撞出怎样绚丽的火花呢?今天,我们... 目录方法1:C++/CLI搭建桥梁——Java ↔ C# 的“翻译官”步骤1:创建C#类库(.NET

MyBatis编写嵌套子查询的动态SQL实践详解

《MyBatis编写嵌套子查询的动态SQL实践详解》在Java生态中,MyBatis作为一款优秀的ORM框架,广泛应用于数据库操作,本文将深入探讨如何在MyBatis中编写嵌套子查询的动态SQL,并结... 目录一、Myhttp://www.chinasem.cnBATis动态SQL的核心优势1. 灵活性与可

Mybatis嵌套子查询动态SQL编写实践

《Mybatis嵌套子查询动态SQL编写实践》:本文主要介绍Mybatis嵌套子查询动态SQL编写方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、实体类1、主类2、子类二、Mapper三、XML四、详解总结前言MyBATis的xml文件编写动态SQL

C++ RabbitMq消息队列组件详解

《C++RabbitMq消息队列组件详解》:本文主要介绍C++RabbitMq消息队列组件的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. RabbitMq介绍2. 安装RabbitMQ3. 安装 RabbitMQ 的 C++客户端库4. A

SpringBoot实现Kafka动态反序列化的完整代码

《SpringBoot实现Kafka动态反序列化的完整代码》在分布式系统中,Kafka作为高吞吐量的消息队列,常常需要处理来自不同主题(Topic)的异构数据,不同的业务场景可能要求对同一消费者组内的... 目录引言一、问题背景1.1 动态反序列化的需求1.2 常见问题二、动态反序列化的核心方案2.1 ht

golang实现延迟队列(delay queue)的两种实现

《golang实现延迟队列(delayqueue)的两种实现》本文主要介绍了golang实现延迟队列(delayqueue)的两种实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录1 延迟队列:邮件提醒、订单自动取消2 实现2.1 simplChina编程e简单版:go自带的time

golang实现动态路由的项目实践

《golang实现动态路由的项目实践》本文主要介绍了golang实现动态路由项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习... 目录一、动态路由1.结构体(数据库的定义)2.预加载preload3.添加关联的方法一、动态路由1

无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案

《无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案》:本文主要介绍了无法启动此程序,详细内容请阅读本文,希望能对你有所帮助... 在计算机使用过程中,我们经常会遇到一些错误提示,其中之一就是"api-ms-win-core-path-l1-1-0.dll丢失

Python Selenium动态渲染页面和抓取的使用指南

《PythonSelenium动态渲染页面和抓取的使用指南》在Web数据采集领域,动态渲染页面已成为现代网站的主流形式,本文将从技术原理,环境配置,核心功能系统讲解Selenium在Python动态... 目录一、Selenium技术架构解析二、环境搭建与基础配置1. 组件安装2. 驱动配置3. 基础操作模