POJ 2373 Dividing the Path(线段树+dp)

本博客探讨了FJ在田埂上安装喷头以覆盖特定区域的最小数量问题,涉及喷头覆盖范围、喷头间距及喷水区域需求。通过实例解释了解决方案,包括输入参数解析、构建数据结构、更新与查询过程,最终输出所需喷头的最小数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dividing the Path
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3798 Accepted: 1363

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



参考博客 http://www.2cto.com/kf/201208/147513.html

上面博客已经比较清楚


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-8
typedef long long ll;

using namespace std;
#define N  1000005
#define INF 0x3f3f3f3f

struct stud{
    int le,ri,va;
}f[N*4];

int lena,lenb;
int a[N];
int n,all;

inline void pushup(int pos)
{
    f[pos].va=min(f[L(pos)].va,f[R(pos)].va);
}

void build(int pos,int le,int ri)
{
    f[pos].le=le;
    f[pos].ri=ri;
    if(le==ri)
    {
        f[pos].va=a[le];
        return ;
    }
    int mid=MID(le,ri);
    build(L(pos),le,mid);
    build(R(pos),mid+1,ri);
    pushup(pos);
}

void update(int pos,int le,int va)
{
    if(f[pos].le==le&&f[pos].ri==le)
        {
            f[pos].va=va;
           return ;
        }
    int mid=MID(f[pos].le,f[pos].ri);

    if(mid>=le)
        update(L(pos),le,va);
    else
        update(R(pos),le,va);
    pushup(pos);
}

int query(int pos,int le,int ri)
{
    if(f[pos].le==le&&f[pos].ri==ri)
        return f[pos].va;
    int mid=MID(f[pos].le,f[pos].ri);
     if(mid>=ri) return query(L(pos),le,ri);
     else if(mid<le) return query(R(pos),le,ri);
     return min(query(L(pos),le,mid),query(R(pos),mid+1,ri));
}

int solve()
{
   int i,j;
   if(all&1) return -1;
   if(lenb<1) return -1;
   for(i=1;i<=all;i+=2)
       a[i]=all+1;
   a[0]=0;
   build(1,0,all);

   for(i=2;i<=all;i+=2)
   {
      if(a[i]>all) continue;
      int le=i-lenb*2;
      int ri=i-lena*2;
      le=max(0,le);
      if(ri<0) continue;
      int temp=query(1,le,ri);
      a[i]=temp+1;
      update(1,i,a[i]);
   }
   if(a[all]>all)
    return -1;
   return a[all];
}

int main()
{
    int i,j;

    while(~scanf("%d%d",&n,&all))
    {
       scanf("%d%d",&lena,&lenb);
       int s,e;
       for(i=0;i<=all;i++) a[i]=all;
       while(n--)
       {
         scanf("%d%d",&s,&e);
         for(i=s+1;i<e;i++)
            a[i]=all+1;
       }
       a[0]=0;
       printf("%d\n",solve());
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值