前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ 2374 Fence Obstacle Course(线段树+动态规划)

POJ 2374 Fence Obstacle Course(线段树+动态规划)

作者头像
ShenduCC
发布2018-04-27 11:03:58
6430
发布2018-04-27 11:03:58
举报
文章被收录于专栏:算法修养算法修养

Fence Obstacle Course

Time Limit: 3000MS

Memory Limit: 65536K

Total Submissions: 2524

Accepted: 910

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). 

代码语言:javascript
复制
   +-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

代码语言:javascript
复制
4 0 
-2 1
-1 2
-3 0
-2 1

Sample Output

代码语言:javascript
复制
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: 

代码语言:javascript
复制
   +-+-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.

动态规划,利用线段树找出每一段的两个端点直直落下可以到达的层数,然后在线段树中覆盖这一段区间。

累死于区间染色。

代码语言:javascript
复制
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;
const int INF=1e9;
const int maxn=1e5;
int n,s;
int a[maxn*2+5];
int b[maxn*2+5];
int dp[maxn*2+5][2];
int cover[maxn*8+5];
void pushdown(int node)
{
	if(cover[node]!=0)
	{
		cover[node<<1]=cover[node];
		cover[node<<1|1]=cover[node];
		cover[node]=0;
	}
}
void update(int node,int l,int r,int L,int R,int tag)
{
	if(L<=l&&r<=R)
	{
		cover[node]=tag;
		return;
	}
	pushdown(node);
	int mid=(l+r)>>1;
	if(L<=mid) update(node<<1,l,mid,L,R,tag);
	if(R>mid) update(node<<1|1,mid+1,r,L,R,tag);
}
int query(int node,int l,int r,int tag)
{
	if(l==r)
	{
		return cover[node];
	}
	pushdown(node);
	int mid=(l+r)>>1;
	if(tag<=mid) return query(node<<1,l,mid,tag);
	else return query(node<<1|1,mid+1,r,tag);
}
int main()
{
	scanf("%d%d",&n,&s);
	s+=maxn;
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i],&b[i]);
		a[i]+=maxn;b[i]+=maxn;
	}
	memset(cover,0,sizeof(cover));
	memset(dp,0,sizeof(dp));
	for(int i=1;i<=n;i++)
	{
		int x=query(1,1,maxn*2,a[i]);
		int y=query(1,1,maxn*2,b[i]);
		if(x==0) dp[i][0]=abs(a[i]-maxn);
		else dp[i][0]=min(dp[x][0]+abs(a[i]-a[x]),dp[x][1]+abs(a[i]-b[x]));
		if(y==0) dp[i][1]=abs(b[i]-maxn);
		else dp[i][1]=min(dp[y][0]+abs(b[i]-a[y]),dp[y][1]+abs(b[i]-b[y]));
		update(1,1,maxn*2,a[i],b[i],i);
	}
	printf("%d\n",min(dp[n][0]+abs(s-a[n]),dp[n][1]+abs(s-b[n])));
	return 0;

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-11-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档