POJ2374-Fence Obstacle Course

Fence Obstacle Course
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 2612 Accepted: 946

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



题意: 给出一些篱笆,编号为1~n,1号篱笆在最下面,n号篱笆在最上面,给出起点s的坐标,要求牛从起点出发,到达到达n号篱笆下面的地上的0点,求出最小横向路径

解题思路:线段树+dp,按输入将篱笆插入线段树种,用线段树维护每个区间能到哪一个篱笆,dp维护每个篱笆到终点的距离


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int x1[50009],x2[50009];
int x[200009*4],n,s;
int dp[50009][2];

void update(int k,int l,int r,int ll,int rr,int val)
{
    if(l>=ll&&r<=rr) {x[k]=val;return ;}
    int mid=(l+r)>>1;
    if(x[k]) {x[k<<1]=x[k<<1|1]=x[k],x[k]=0;}
    if(ll<=mid) update(k<<1,l,mid,ll,rr,val);
    if(rr>mid) update(k<<1|1,mid+1,r,ll,rr,val);
}

int query(int k,int l,int r,int p)
{
    if(x[k]||l==r) return x[k];
    int mid=(l+r)>>1;
    if(p<=mid) return query(k<<1,l,mid,p);
    else return query(k<<1|1,mid+1,r,p);
}

int main()
{
    while(~scanf("%d%d",&n,&s))
    {
        memset(x,0,sizeof x);
        dp[0][0]=dp[0][1]=0;
        x1[0]=x2[0];
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&x1[i],&x2[i]);
            int a=query(1,0,200000,x1[i]+100000);
            int b=query(1,0,200000,x2[i]+100000);
            dp[i][0]=min(abs(x1[a]-x1[i])+dp[a][0],dp[a][1]+abs(x2[a]-x1[i]));
            dp[i][1]=min(abs(x1[b]-x2[i])+dp[b][0],dp[b][1]+abs(x2[b]-x2[i]));
            update(1,0,200000,x1[i]+100000,x2[i]+100000,i);
        }
        printf("%d\n",min(dp[n][0]+abs(s-x1[n]),dp[n][1]+abs(s-x2[n])));
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值