首页 技术 正文
技术 2022年11月14日
0 收藏 388 点赞 5,022 浏览 3404 个字

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\n",min(dp[n][0]+abs(s-a[n]),dp[n][1]+abs(s-b[n])));
return 0;}

上一篇: Paginator
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849