百练#2373Dividing the Path

描述
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.
输入

  • 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.
    输出

  • Line 1: The minimum number of sprinklers required. If it is not possible to design a sprinkler head configuration for Farmer John, output -1.
    样例输入
    2 8
    1 2
    6 7
    3 6
    样例输出
    3

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<utility>
using namespace std;
const int MAXL = 1000010;
const int MAXN = 1010;
int d[MAXL];//d[i]终点为i,需要的喷头最少值。
int cow[MAXL];//cow[i] = 1,i处不能作为分界线,因为牛偏好的区域不能被分开 
typedef pair<int,int> P;
priority_queue<P,vector<P>,greater<P> > heap;
int main(){
	int n,l,a,b,i,j,s,e;
	P p;
	scanf("%d%d%d%d",&n,&l,&a,&b);
	memset(cow,0,sizeof(cow));
	for(i = 0;i < n; ++i){
		scanf("%d%d",&s,&e);
		for(j = s + 1;j < e; ++j)
			cow[j] = 1;//书上求cow有点搞笑,遂改了一哈。
	}
	memset(d,-1,sizeof(d));
	int a2 = a * 2;
	int b2 = b * 2;
	for(i = a2;i <= l;i += 2){
		if(i <= b2){
			if(!cow[i]){//书上这样处理首个区间也是蛮清晰的。
				d[i] = 1;
				if(i <= b2 + 2 - a2)
					heap.push(P(d[i],i));	
			}
			continue;
		}
		else{
			if(!cow[i]){
				while(!heap.empty()){
					p = heap.top();
					if(p.second >= i - b2){
						break;//如果该点是当前遍历位置的前一个分界点,当然不需要出队,也可能是以后某个位置的前一个分界点。WA1次
					}
					heap.pop();
				}
				if(!heap.empty()){
					d[i] = p.first + 1;
				}
			}
			if(d[i - a2 + 2] >= 0)//书上的方法的精髓。[1]
				heap.push(P(d[i - a2 + 2],i - a2 + 2));
		}
	}
	printf("%d",d[l]);
	return 0;
} 

[1]WA3次,因为不管有没有牛,都要将下一个位置的前一个区间最右侧位置(存在)入堆。

以后WA该怎么检查呢?还得写的时候思路清晰呀,到了检查的时候那就输了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值