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


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

    动态规划,利用线段树找出每一段的两个端点直直落下可以到达的层数,然后在线段树中覆盖这一段区间。
    累死于区间染色。
    #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
    ",min(dp[n][0]+abs(s-a[n]),dp[n][1]+abs(s-b[n])));
    	return 0;
    
    }



  • 相关阅读:
    js入门简单介绍
    HTML中input参数,多行文本textarea说明,以及获取和设置的方法
    css属性相对定位,绝对定位,固定定位
    Django框架(二十七)—— ContentType组件
    Django框架(二十八)—— Django缓存机制
    Django框架(二十五)—— Django rest_framework-路由控制与响应器
    Django框架(二十六)—— Django rest_framework-分页器与版本控制
    Django框架(二十三)—— Django rest_framework-解析器
    Django框架(二十四)—— Django rest_framework-视图组件
    Django框架(二十二)—— Django rest_framework-频率组件
  • 原文地址:https://www.cnblogs.com/dacc123/p/8228572.html
Copyright © 2020-2023  润新知