POJ2374 Fence Obstacle Course

题意


Language:Default

Fence Obstacle Course

Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 2900 Accepted: 1042

Description

Farmer John has constructed an obstacle course for the cows‘ enjoyment. The course consists of a sequence of N fences (1 <= N <= 50,000) of varying lengths, each parallel to the x axis. Fence i‘s y coordinate is i.

The door to FJ‘s barn is at the origin (marked ‘*‘ below). The starting point of the course lies at coordinate (S,N).

   +-S-+-+-+        (fence #N)
 +-+-+-+            (fence #N-1)
     ...               ...
   +-+-+-+          (fence #2)
     +-+-+-+        (fence #1)
=|=|=|=*=|=|=|      (barn)
-3-2-1 0 1 2 3    

FJ‘s original intention was for the cows to jump over the fences, but cows are much more comfortable keeping all four hooves on the ground. Thus, they will walk along the fence and, when the fence ends, they will turn towards the x axis and continue walking in a straight line until they hit another fence segment or the side of the barn. Then they decide to go left or right until they reach the end of the fence segment, and so on, until they finally reach the side of the barn and then, potentially after a short walk, the ending point.

Naturally, the cows want to walk as little as possible. Find the minimum distance the cows have to travel back and forth to get from the starting point to the door of the barn.

Input

* Line 1: Two space-separated integers: N and S (-100,000 <= S <= 100,000)

* Lines 2..N+1: Each line contains two space-separated integers: A_i and B_i (-100,000 <= A_i < B_i <= 100,000), the starting and ending x-coordinates of fence segment i. Line 2 describes fence #1; line 3 describes fence #2; and so on. The starting position will satisfy A_N <= S <= B_N. Note that the fences will be traversed in reverse order of the input sequence.

Output

* Line 1: The minimum distance back and forth in the x direction required to get from the starting point to the ending point by walking around the fences. The distance in the y direction is not counted, since it is always precisely N.

Sample Input

4 0
-2 1
-1 2
-3 0
-2 1

Sample Output

4

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

INPUT DETAILS:

Four segments like this:

   +-+-S-+             Fence 4
 +-+-+-+               Fence 3
     +-+-+-+           Fence 2
   +-+-+-+             Fence 1
 |=|=|=*=|=|=|         Barn
-3-2-1 0 1 2 3      

OUTPUT DETAILS:

Walk positive one unit (to 1,4), then head toward the barn, trivially going around fence 3. Walk positive one more unit (to 2,2), then walk to the side of the barn. Walk two more units toward the origin for a total of 4 units of back-and-forth walking.

Source

USACO 2004 December Gold

分析

参照逐梦起航-带梦飞翔的题解,线段树优化DP

设f[i][0/1]表示在通过第i条栅栏后,处于栅栏左边/右边的最小路径长。

因为奶牛是直线下来的,所以最优方案当然是从上一个栅栏的这个位置下来。由于有栅栏的影响,奶牛们不能顺利的下来,此时到达这个位置的最优策略要么是从前面那个栅栏的左端点过来,要么从右端点过来。所以有
\[
f[i][0]=\min\{f[j][0]+|l_i-l_j|,f[j][1]+|l_i-r_j|\} \f[i][1]=\min\{f[j][0]+|r_i-l_j|,f[j][1]+|r_i-r_j|\}
\]
其中的j就是上一个挡住了这个位置的栅栏。我们可以用线段树来维护这个栅栏的编号。当栅栏(l[i],r[i]),出现后,我们把线段树上(l[i],r[i])这段区间改成i,表示这个位置是栅栏i阻挡了。对于后面的栅栏,修改时直接覆盖前面的信息即可。我们只要实现一个改段求点的线段树即可。

特别的,线段树初始值为0。一个位置如果得到的j=0,那么说明它前面没有栅栏,它可以直接从s过来,路径=abs(s-p)。

时间复杂度\(O(n\log s)\),也可以用线段树连边跑最短路,但这题用DP来做常数小。

代码

#include<iostream>
#include<cmath>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
    rg T data=0,w=1;rg char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
    while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
    return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std;

co int N=5e4+1,S=2e5+1,X=1e5;
int n,s,l[N],r[N],f[N][2];
struct T {int l,r,x;}t[S*4];
void build(int p,int l,int r){
    t[p].l=l,t[p].r=r,t[p].x=l==r?0:-1;
    if(l==r) return;
    int mid=l+r>>1;
    build(p<<1,l,mid),build(p<<1|1,mid+1,r);
}
void change(int p,int l,int r,int x){
    if(l<=t[p].l&&t[p].r<=r) return t[p].x=x,void();
    if(t[p].x!=-1) t[p<<1].x=t[p<<1|1].x=t[p].x,t[p].x=-1;
    int mid=t[p].l+t[p].r>>1;
    if(l<=mid) change(p<<1,l,r,x);
    if(r>mid) change(p<<1|1,l,r,x);
}
int ask(int p,int x){
    if(t[p].l==t[p].r) return t[p].x;
    if(t[p].x!=-1) t[p<<1].x=t[p<<1|1].x=t[p].x,t[p].x=-1;
    int mid=t[p].l+t[p].r>>1;
    return ask(x<=mid?p<<1:p<<1|1,x);
}
int main(){
    read(n),read(s);
    build(1,0,X*2);
    s+=X,l[0]=r[0]=X;
    for(int i=1,w;i<=n;++i){
        l[i]=read<int>()+X,r[i]=read<int>()+X;
        w=ask(1,l[i]);
        f[i][0]=min(f[w][0]+abs(l[i]-l[w]),f[w][1]+abs(l[i]-r[w]));
        w=ask(1,r[i]);
        f[i][1]=min(f[w][0]+abs(r[i]-l[w]),f[w][1]+abs(r[i]-r[w]));
        change(1,l[i],r[i],i);
    }
    printf("%d\n",min(f[n][0]+s-l[n],f[n][1]+r[n]-s));
    return 0;
}

原文地址:https://www.cnblogs.com/autoint/p/10794492.html

时间: 2024-10-20 06:50:50

POJ2374 Fence Obstacle Course的相关文章

POJ2374 Fence Obstacle Course 【线段树】

题目链接 POJ2374 题解 题意: 给出\(n\)个平行于\(x\)轴的栅栏,求从一侧栅栏的某个位置出发,绕过所有栅栏到达另一侧\(x = 0\)位置的最短水平距离 往上说都是线段树优化dp 我写了一个奇怪的线段树过了,似乎并没有和dp沾边 因为每次都是从某个栅栏的端点出发,到达某个位置的值等于[所有这些可出发的端点已产生的代价 + 到达这个点的距离] 的最小值 就用线段树维护每个区间 \(lm[u]\)表示在这个区间内的端点到达左端点的最小代价 \(rm[u]\)表示到右端点的最小代价 然

Fence Obstacle Course

Fence Obstacle Course 有n个区间自下而上有顺序的排列,标号\(1\sim n\),第i个区间记做\([l_i,r_i]\),现在从第n个区间的起点s出发(显然s在\([l_n,r_n]\)内),每次可以选择移动到所在区间的左端点或者右端点,然后跳下去,到达第一个碰到的区间,继续进行相同操作,定义第0个区间为无限延伸,求到第0个区间的位置0的最小水平移动距离,\(n\leq 50000,-1000000\leq l_i,r_i\leq 100000\). 解 思路一 注意到每

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

poj 动态规划题目列表及总结

此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276,1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740(博弈),1742, 1887, 1926(马尔科夫矩阵,求平衡), 1936, 1952, 1953, 1958, 1959, 1962, 1975,

DP题目列表/弟屁专题

声明: 1.这份列表不是我原创的,放到这里便于自己浏览和查找题目. ※最近更新:Poj斜率优化题目 1180,2018,3709 列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740, 1742, 1887, 1926, 1936, 195

[转] POJ DP问题

列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740, 1742, 1887, 1926, 1936, 1952, 1953, 1958, 1959, 1962, 1975, 1989, 2018, 2029, 2039, 2063, 20

[POJ3253] Fence Repair

Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a s