程设---Dividing the Path

Posted by Kevin on June 16, 2022

描述

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.


solution

令 $f[i]$ 表示覆盖 $0 \sim i$ 所需要的最少喷头数

$f[i] = f[j] + 1 [cover(j+1,i)]$

$ cover(l,r) $ 判断 $l \sim r$ 能否被一个喷头覆盖

转移具有单调性(若 $f[i] = f[j] \&\& i < j$ ,则从 $j$ 转移一定更优),单调队列优化

实现的时候用差分来判断一个位置可不可以放

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#define inf 1e9
using namespace std;
const int MaxL=1000005;
int n,L,A,B,f[MaxL];
int flag[MaxL];
int l,r;
struct node{
	int l,r;
}s[1005],q[MaxL]; 
void init(){
	scanf("%d%d",&n,&L);
	scanf("%d%d",&A,&B);A*=2,B*=2;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&s[i].l,&s[i].r);
		flag[s[i].l+1]++;flag[s[i].r]--;
	}
	for(int i=1;i<=L;i++)flag[i]+=flag[i-1];
}
void work(){
	l=0,r=1;
	q[r]=(node){0,0};
	for(int i=2;i<=L;i+=2){
		f[i]=inf;
		if(flag[i]>0)continue;
		while(l<r){
			node t=q[l+1];
			if(i-t.l<A)break;
			if(i-t.l>B){
				l++;continue;
			}
			f[i]=t.r+1;
			q[++r]=(node){i,f[i]};
			break;
		}
//		cout<<i<<' '<<f[i]<<endl;
	}
	if(f[L]==inf)puts("-1");
	else printf("%d\n",f[L]);
}
int main(){
	init();
	work();
	return 0;
}