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; }


POJ 2374 Fence Obstacle Course(线段树+动态规划)的更多相关文章

  1. poj2374 Fence Obstacle Course[线段树+DP]

    https://vjudge.net/problem/POJ-2374 吐槽.在这题上面磕了许久..英文不好题面读错了qwq,写了个错的算法搞了很久..A掉之后瞥了一眼众多julao题解,**,怎么想 ...

  2. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  3. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  4. POJ 2528 Mayor's posters (线段树)

    题目链接:http://poj.org/problem?id=2528 题目大意:有一个很上的面板, 往上面贴海报, 问最后最多有多少个海报没有被完全覆盖 解题思路:将贴海报倒着想, 对于每一张海报只 ...

  5. POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 D ...

  6. POJ 2777 Count Color(线段树染色,二进制优化)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42940   Accepted: 13011 Des ...

  7. poj 2528 Mayor's posters(线段树)

    题目:http://poj.org/problem?id=2528 题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度.现在往墙上贴N张海报,每张海报的宽度是任意的, 但是必定是单位宽度的整数 ...

  8. POJ 2528 Mayor's posters (线段树区间更新+离散化)

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  9. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

随机推荐

  1. Java字节码指令

    1. 简介 Java虚拟机的指令由一个字节长度的.代表着某种特定操作含义的数字(称为操作码)以及跟随其后的零至多个代表此操作所需参数(称为操作数)而构成. 由于Java虚拟机采用面向操作数栈而不是寄存 ...

  2. 50篇经典珍藏 | Docker、Mesos、微服务、云原生技术干货

    概念篇 全方位探(tian)索(keng)Mesos各种存储处理方式 老肖有话说@Mesos User Group第四次约会 技术实践 | Mesos 全方位“烹饪”指南 回顾 JAVA 发展轨迹,看 ...

  3. NYOJ 467 中缀式变后缀式

    做了表达式求值那道题之后做的 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 人们的日常习惯是把算术表达式写成中缀式,但对于机器来说更"习惯于"后 ...

  4. $q服务的使用

    1. 创建一个Service,去服务器读取数据: // $q 是内置服务,所以可以直接使用 ngApp.factory('UserInfo', ['$http', '$q', function ($h ...

  5. [转]对P,NP和NPC问题的解释

    总结: 归约(或别的什么叫法):如果解决了问题A,就能用解决A的方法来解决问题B,那么我们说问题B可以归约为/到问题A,本文记为[B]<[A].其含义就是问题A的求解复杂度比问题B要高,比如说A ...

  6. 访问子节点childNodes

    访问子节点childNodes 访问选定元素节点下的所有子节点的列表,返回的值可以看作是一个数组,他具有length属性. 语法: elementNode.childNodes 注意: 如果选定的节点 ...

  7. SSL/TLS协议运行机制的概述_转

    转自:SSL/TLS协议运行机制的概述 作者: 阮一峰 日期: 2014年2月 5日 互联网的通信安全,建立在SSL/TLS协议之上. 本文简要介绍SSL/TLS协议的运行机制.文章的重点是设计思想和 ...

  8. Hdu 2236 无题II 最大匹配+二分

    题目链接: pid=2236">Hdu 2236 解题思路: 将行和列理解为二分图两边的端点,给出的矩阵即为二分图中的全部边, 假设二分图能全然匹配,则说明 不同行 不同列的n个元素 ...

  9. Requests blocked by CORS policy in spring boot and angular

    在本地启动Spring Boot后端和Angular前端调试时遇到跨域访问的问题导致前端请求失败. 错误描述 Access to XMLHttpRequest at 'http://localhost ...

  10. mui区域滚动条

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...