poj 2373 Dividing the Path

http://poj.org/problem?id=2373

 

题意:

在长为L的草地上装喷水头,喷水头的喷洒半径为[a,b]
要求草地的每个整点被且只被一个喷水头覆盖
有N个特殊区间,要求只能被某一个喷水头完整地覆盖,而不能由多个喷水头分段覆盖
求喷水头的最小数目
喷水头只能建在整数点上
 
f[i] 表示区间[0,i]完全被覆盖的最少喷水头数目
f[i]=min(f[j]+1)   j∈[i-2*a,i-2*b]
若i处于某个特殊区间内部,则f[i]=inf
单调队列优化
 
注意因为喷水头只能建在整数点上
当i为奇数时,区间[0,i]一共有偶数个点,喷水头不可能建在整数点上
所以只有i为偶数时才是有用的
 
这题真心不好写!!!
 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

int a,b;

int dp[1000001];
bool all[1000001];

struct node
{
    int l,r;
}e[1001];

int q[1000001];
int h,t;

void read(int &x)
{
    x=0; char c=getchar();
    while(!isdigit(c)) c=getchar(); 
    while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); }
}

bool cmp(node p,node q)
{
    if(p.l!=q.l) return p.l<q.l;
    return p.r<q.r;
}

void up(int i)
{
    while(h<t && i-q[h]>2*b) h++;
    if(h<t && i-q[h]>=2*a)
    {
        dp[i]=dp[q[h]]+1;
        while(h<t && dp[i]<dp[q[t-1]]) t--;
        q[t++]=i;
    }
}

int main()
{
    #ifndef ONLINE_JUDGE 
        freopen("data.in","r",stdin);
        freopen("xxy.out","w",stdout);
    #endif
    int n,l;
    read(n); read(l);
    read(a); read(b);
    for(int i=1;i<=n;++i) read(e[i].l),read(e[i].r);
    sort(e+1,e+n+1,cmp);
    memset(dp,63,sizeof(dp));
    int INF=dp[0];
    dp[0]=0;
    q[t++]=0;
    int now=1;
    int i=1;
    while(i<=l)
    {
        if(now<=n && e[now].l<i)
        {
            if(e[now].r>i) i=e[now].r;
            now++;
            continue;
        }
        if(!(i&1)) up(i);
        i++;
    }
    if(dp[l]==INF) printf("-1");
    else printf("%d",dp[l]);
}

 

Dividing the Path
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5148 Accepted: 1803

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.
 
 

转载于:https://www.cnblogs.com/TheRoadToTheGold/p/8386156.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值