POJ 2374 Fence Obstacle Course

98 篇文章 0 订阅
52 篇文章 0 订阅

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.


对于每一层,用线段树快速求出其左右端点对应的层数,然后dp

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 50005;
int n, m, dp[maxn][2], dis[maxn][2], f[maxn * 16];

void build(int x, int l, int r)
{
	f[x] = 0;
	if (l < r) 
	{
		int mid = (l + r) >> 1;
		build(x + x, l, mid);
		build(x + x + 1, mid + 1, r);
	}
}

int get(int x, int l, int r, int now)
{
	if (f[x] || l == r) return f[x];
	int mid = (l + r) >> 1;
	if (now > mid) return get(x + x + 1, mid + 1, r, now);
	else return get(x + x, l, mid, now);
}

int down(int x)
{
	return get(1, 0, 200000, x + 100000);
}

void add(int x, int l, int r, int ll, int rr, int v)
{
	if (ll <= l&&r <= rr) { f[x] = v; return; }
	int mid = (l + r) >> 1;
	if (f[x]) { f[x + x] = f[x + x + 1] = f[x]; f[x] = 0; }
	if (ll <= mid) add(x + x, l, mid, ll, rr, v);
	if (rr > mid) add(x + x + 1, mid + 1, r, ll, rr, v);
}

void insert(int x, int l, int r)
{
	add(1, 0, 200000, l + 100000, r + 100000, x);
}

int main()
{
	while (scanf("%d%d", &n, &m) != EOF)
	{
		build(1, 0, 200000);
		dp[0][0] = dp[0][1] = 0;
		dis[0][0] = dis[0][1] = 0;
		for (int i = 1; i <= n; i++)
		{
			scanf("%d%d", &dis[i][0], &dis[i][1]);
			int x = down(dis[i][0]), y = down(dis[i][1]);
			dp[i][0] = min(dp[x][0] + abs(dis[i][0] - dis[x][0]), dp[x][1] + abs(dis[x][1] - dis[i][0]));
			dp[i][1] = min(dp[y][0] + abs(dis[i][1] - dis[y][0]), dp[y][1] + abs(dis[y][1] - dis[i][1]));
			insert(i, dis[i][0], dis[i][1]);
		}
		printf("%d\n", min(dp[n][0] + m - dis[n][0], dp[n][1] + dis[n][1] - m));
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值