POJ 2374 Fence Obstacle Course(线段树+动态规划)
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 2524 | Accepted: 910 |
Description
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
* 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
Sample Input
4 0
-2 1
-1 2
-3 0
-2 1
Sample Output
4
Hint
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(线段树+动态规划)的更多相关文章
- poj2374 Fence Obstacle Course[线段树+DP]
https://vjudge.net/problem/POJ-2374 吐槽.在这题上面磕了许久..英文不好题面读错了qwq,写了个错的算法搞了很久..A掉之后瞥了一眼众多julao题解,**,怎么想 ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- POJ 2528 Mayor's posters (线段树)
题目链接:http://poj.org/problem?id=2528 题目大意:有一个很上的面板, 往上面贴海报, 问最后最多有多少个海报没有被完全覆盖 解题思路:将贴海报倒着想, 对于每一张海报只 ...
- POJ 2892 Tunnel Warfare(线段树单点更新区间合并)
Tunnel Warfare Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7876 Accepted: 3259 D ...
- POJ 2777 Count Color(线段树染色,二进制优化)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42940 Accepted: 13011 Des ...
- poj 2528 Mayor's posters(线段树)
题目:http://poj.org/problem?id=2528 题意:有一面墙,被等分为1QW份,一份的宽度为一个单位宽度.现在往墙上贴N张海报,每张海报的宽度是任意的, 但是必定是单位宽度的整数 ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
随机推荐
- 为什么会找不到D层文件?
近期两天在重装系统,今天好不easy把各种东西都装齐全了,再打开我的机房收费系统,就提演示样例如以下错误: 看到这个问题.我感觉非常熟,由于曾经也遇到过两次这个问题,都是改了下D层的编译路径.改到了U ...
- Java并发之彻底搞懂偏向锁升级为轻量级锁
网上有许多讲偏向锁,轻量级锁的文章,但对偏向锁如何升级讲的不够明白,有些文章还相互矛盾,经过对jvm源码(biasedLocking.cpp)的仔细分析和追踪,基本升级过程有了一个清晰的过程,现将升级 ...
- 偏于SQL语句的 sqlAlchemy 增删改查操作
ORM 江湖 曾几何时,程序员因为惧怕SQL而在开发的时候小心翼翼的写着sql,心中总是少不了恐慌,万一不小心sql语句出错,搞坏了数据库怎么办?又或者为了获取一些数据,什么内外左右连接,函数存储过程 ...
- poj2774(后缀数组水题)
http://poj.org/problem?id=2774 题意:给你两串字符,要你找出在这两串字符中都出现过的最长子串......... 思路:先用个分隔符将两个字符串连接起来,再用后缀数组求出h ...
- poj2559单调栈
题意:给出连续的矩形的高....求最大面积 #include<iostream> #include<stack> #include<stdio.h> using n ...
- SQLite的连接字符串
SQLite的连接字符串 Basic(基本的) Data Source=filename;Version=3;Using UTF16(使用UTF16编码) Data Source=fil ...
- Apache ab使用POST参数进行压力测试 (服务端为Django)
2016年07月07日 15:04:51 常城 阅读数:13774更多 个人分类: PythonLinux架构 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...
- Differential Geometry之第六章平面曲线的整体性质
第六章.平面曲线的整体性质 1.平面的闭曲线 1.1.切线的旋转指数定理 1.2.等周不等式与圆的几何特性 ,其中 2.平面的凸曲线 支撑函数: 2.1.Minkowski问题 2.2.四顶点定理
- Unity四元数和旋转
四元数介绍 旋转,应该是三种坐标变换——缩放.旋转和平移,中最复杂的一种了.大家应该都听过,有一种旋转的表示方法叫四元数.按照我们的习惯,我们更加熟悉的是另外两种旋转的表示方法——矩阵旋转和欧拉旋转. ...
- media wiki run on nginx
1. 环境安装: nginx安装 nginx-1.5.7 php安装 PHP 5.4.10 (cli) (built: Jul 30 2014 16:45:08) mysql安装 Ver 14.14 ...