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

相关文章

Vue中动态权限到按钮的完整实现方案详解

《Vue中动态权限到按钮的完整实现方案详解》这篇文章主要为大家详细介绍了Vue如何在现有方案的基础上加入对路由的增、删、改、查权限控制,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、数据库设计扩展1.1 修改路由表(routes)1.2 修改角色与路由权限表(role_routes)二、后端接口设计

Spring Boot整合消息队列RabbitMQ的实现示例

《SpringBoot整合消息队列RabbitMQ的实现示例》本文主要介绍了SpringBoot整合消息队列RabbitMQ的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的... 目录RabbitMQ 简介与安装1. RabbitMQ 简介2. RabbitMQ 安装Spring

前端 CSS 动态设置样式::class、:style 等技巧(推荐)

《前端CSS动态设置样式::class、:style等技巧(推荐)》:本文主要介绍了Vue.js中动态绑定类名和内联样式的两种方法:对象语法和数组语法,通过对象语法,可以根据条件动态切换类名或样式;通过数组语法,可以同时绑定多个类名或样式,此外,还可以结合计算属性来生成复杂的类名或样式对象,详细内容请阅读本文,希望能对你有所帮助...

Nginx实现动态封禁IP的步骤指南

《Nginx实现动态封禁IP的步骤指南》在日常的生产环境中,网站可能会遭遇恶意请求、DDoS攻击或其他有害的访问行为,为了应对这些情况,动态封禁IP是一项十分重要的安全策略,本篇博客将介绍如何通过NG... 目录1、简述2、实现方式3、使用 fail2ban 动态封禁3.1 安装 fail2ban3.2 配

Vue3中的动态组件详解

《Vue3中的动态组件详解》本文介绍了Vue3中的动态组件,通过`component:is=动态组件名或组件对象/component`来实现根据条件动态渲染不同的组件,此外,还提到了使用`markRa... 目录vue3动态组件动态组件的基本使用第一种写法第二种写法性能优化解决方法总结Vue3动态组件动态

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo

Java使用POI-TL和JFreeChart动态生成Word报告

《Java使用POI-TL和JFreeChart动态生成Word报告》本文介绍了使用POI-TL和JFreeChart生成包含动态数据和图表的Word报告的方法,并分享了实际开发中的踩坑经验,通过代码... 目录前言一、需求背景二、方案分析三、 POI-TL + JFreeChart 实现3.1 Maven

Java导出Excel动态表头的示例详解

《Java导出Excel动态表头的示例详解》这篇文章主要为大家详细介绍了Java导出Excel动态表头的相关知识,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录前言一、效果展示二、代码实现1.固定头实体类2.动态头实现3.导出动态头前言本文只记录大致思路以及做法,代码不进