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

时间: 2024-10-31 01:52:59

poj 2373 Dividing the Path的相关文章

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

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 clo

【POJ】2373 Dividing the Path(单调队列优化dp)

题目 传送门:QWQ 分析 听说是水题,但还是没想出来. $ dp[i] $为$ [1,i] $的需要的喷头数量. 那么$ dp[i]=min(dp[j])+1 $其中$ j<i $ 这是个$ O(n^2)$的东西,用单调队列优化一下就行了 复杂度$ O(L) $ 代码 #include <bits/stdc++.h> using namespace std; const int maxn=250; int A[maxn][maxn], num[maxn*maxn], id[100000

poj 3764 The xor-longest Path(字典树)

题目链接:poj 3764 The xor-longest Path 题目大意:给定一棵树,每条边上有一个权值,找出一条路径,使得路径上权值的亦或和最大. 解题思路:dfs一遍,预处理出每个节点到根节点路径的亦或和rec,那么任意路径均可以表示rec[a] ^ rec[b],所以问题 就转换成在一些数中选出两个数亦或和最大,那么就建立字典树查询即可. #include <cstdio> #include <cstring> #include <algorithm> us

poj 1004 Dividing

题目大意是,从输入六个数 ,第i个数代表价值为i的有几个,平均分给两个人 ,明摆着的背包问题,本来以为把他转化为01背包,但是TLe,后来发现是12万的平方还多,所以妥妥的TLE,后来发现这是一个完全背包问题,然后即纠结了 ,没学过啊 ,最后发现思想好i是蛮简单的,水水的A掉了,最后注意一下初始化问题和输入问题后就好了 #include <stdio.h> #include <string.h> int a[10]; int dp[120005]; int maxx(int a,i

POJ 1014 Dividing 背包

这道题使用多重背包,不过其实我也不太明白为什么叫这个名字. 因为感觉不是什么多重,而是物体的分解问题. 就是比如一个物体有数量限制,比如是13,那么就需要把这个物体分解为1, 2, 4, 6 如果这个物体有数量为25,那么就分解为1, 2, 4, 8, 10 看出规律吗,就是分成2的倍数加上位数,比如6 = 13 - 1 - 2 - 4, 10 = 25 - 1 - 2 - 4 - 8,呵呵,为什么这么分解? 因为这样分解之后就可以组合成所有1到13的数,为25的时候可以组合成所有1到25的数啦

POJ 1014 Dividing (多重背包)

Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 58921   Accepted: 15200 Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbl

POJ 3764 - The xor-longest Path - [DFS+字典树变形]

题目链接:http://poj.org/problem?id=3764 Time Limit: 2000MS Memory Limit: 65536K Description In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of edges on p: $_{xor}length(p) = \bigoplus_{e \in p}w(e)$ $\oplus$

POJ 1014: Dividing

写在前面: 本题非常有意思, 因此我在编程前后和撰写本文前, 在网上查询并阅读了大量关于本题的解题报告. 当中有两种似是而非但提交后却可以 AC 的解法引起了我的兴趣, 一是原作者宣称的 "DFS 解", 二是原作者宣称的 "扩展的找零问题解". 我对这两种解法的源码进行了细致的分析, 找到了它们的问题之所在. 以下我会先对这两种错误解法进行分析和讨论, 然后再给出正确的思路. 错误解法 1: 依照价值从大到小的顺序尝试凑数 (不回溯). 因为裁判的測试数据不够全面

ACM学习历程—POJ 3764 The xor-longest Path(xor &amp;&amp; 字典树 &amp;&amp; 贪心)

题目链接:http://poj.org/problem?id=3764 题目大意是在树上求一条路径,使得xor和最大. 由于是在树上,所以两个结点之间应有唯一路径. 而xor(u, v) = xor(0, u)^xor(0, v). 所以如果预处理出0结点到所有结点的xor路径和,问题就转换成了求n个数中取出两个数,使得xor最大. 这个之前用字典树处理过类似问题. 代码: #include <iostream> #include <cstdio> #include <cst