F - Many Moves


Time limit : 2sec / Memory limit : 256MB

Score : 900 points

Problem Statement

There are N squares in a row. The squares are numbered 1,2,…,N from left to right.

You have two pieces, initially placed on square A and B, respectively. You will be asked to process Q queries of the following kind, in the order received:

  • Given an integer xi, move one of the two pieces of your choice to square xi.

Here, it takes you one second to move a piece one square. That is, the time it takes to move a piece from square X to Y is |XY| seconds.

Your objective is to process all the queries in the shortest possible time.

You may only move the pieces in response to queries, and you may not move both pieces at the same time. Also, it is not allowed to rearrange the order in which queries are given. It is, however, allowed to have both pieces in the same square at the same time.

Constraints

  • 1≤N,Q≤200,000
  • 1≤A,BN
  • 1≤xiN

Input

Input is given from Standard Input in the following format:

N Q A B
x1 x2 ... xQ

Output

Let the shortest possible time to process all the queries be X seconds. Print X.


Sample Input 1

8 3 1 8
3 5 1

Sample Output 1

7

All the queries can be processed in seven seconds, by:

  • moving the piece at square 1 to 3
  • moving the piece at square 8 to 5
  • moving the piece at square 3 to 1

Sample Input 2

9 2 1 9
5 1

Sample Output 2

4

The piece at square 9 should be moved first.


Sample Input 3

9 2 1 9
5 9

Sample Output 3

4

The piece at square 1 should be moved first.


Sample Input 4

11 16 8 1
1 1 5 1 11 4 5 2 5 3 3 3 5 5 6 7

Sample Output 4

21
分析:考虑dp[i][j]表示当前在x[i],j位置;
   设之前一步在a,b,当前到c,d,且a,c为上次和这次到达点;
   那么有a->c或b->c;
   若a->c,则dp[i][j]直接加上abs(x[i]-x[i-1]);
   若b->c,则dp[i][a]取min{dp[i-1][j]+abs(j-x[i])};
   而这两个都可以用线段树维护;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>
#include <ctime>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000009
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
#define ls rt<<1
#define rs rt<<1|1
const int maxn=2e5+;
const int N=2e5+;
using namespace std;
int id(int l,int r){return l+r|l!=r;}
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,k,t,q,a,b;
ll tag[maxn<<],mi[maxn<<],mi1[maxn<<],mi2[maxn<<];
void pup(int rt)
{
mi[rt]=min(mi[ls],mi[rs]);
mi1[rt]=min(mi1[ls],mi1[rs]);
mi2[rt]=min(mi2[ls],mi2[rs]);
tag[rt]=;
}
void pdw(int rt)
{
mi[ls]+=tag[rt];
mi1[ls]+=tag[rt];
mi2[ls]+=tag[rt];
tag[ls]+=tag[rt];
mi[rs]+=tag[rt];
mi1[rs]+=tag[rt];
mi2[rs]+=tag[rt];
tag[rs]+=tag[rt];
tag[rt]=;
}
void build(int l,int r,int rt)
{
if(l==r)
{
mi[rt]=mi1[rt]=mi2[rt]=1e18;
tag[rt]=;
return;
}
int mid=l+r>>;
build(l,mid,ls);
build(mid+,r,rs);
pup(rt);
}
void add(int L,int R,ll v,int l,int r,int rt)
{
if(L==l&&R==r)
{
mi[rt]+=v;
mi1[rt]+=v;
mi2[rt]+=v;
tag[rt]+=v;
return;
}
int mid=l+r>>;
if(tag[rt])pdw(rt);
if(R<=mid)add(L,R,v,l,mid,ls);
else if(L>mid)add(L,R,v,mid+,r,rs);
else
{
add(L,mid,v,l,mid,ls);
add(mid+,R,v,mid+,r,rs);
}
pup(rt);
}
void upd(int pos,ll v,int l,int r,int rt)
{
if(l==pos&&pos==r)
{
if(mi[rt]>v)
{
mi[rt]=v;
mi1[rt]=v-pos;
mi2[rt]=v+pos;
}
return;
}
int mid=l+r>>;
if(tag[rt])pdw(rt);
if(pos<=mid)upd(pos,v,l,mid,ls);
else upd(pos,v,mid+,r,rs);
pup(rt);
}
ll gao(int L,int R,int l,int r,int rt,ll *mi)
{
if(L==l&&R==r)return mi[rt];
int mid=l+r>>;
if(tag[rt])pdw(rt);
if(R<=mid)return gao(L,R,l,mid,ls,mi);
else if(L>mid)return gao(L,R,mid+,r,rs,mi);
else return min(gao(L,mid,l,mid,ls,mi),gao(mid+,R,mid+,r,rs,mi));
}
int main()
{
int i,j;
scanf("%d%d%d%d",&n,&q,&a,&b);
build(,n,);
upd(b,,,n,);
int pre=a;
rep(i,,q)
{
int x;
scanf("%d",&x);
ll cost1=gao(,x,,n,,mi1)+x;
ll cost2=gao(x,n,,n,,mi2)-x;
ll now=min(cost1,cost2);
add(,n,abs(x-pre),,n,);
upd(pre,now,,n,);
pre=x;
}
printf("%lld\n",gao(,n,,n,,mi));
return ;
}

F - Many Moves的更多相关文章

  1. arc073 F many moves(dp + 线段树)

    设dp[i][y]表示一个点在x[i],另一个点在y时最小要走的步数 那么有以下转移 对于y != x[i-1]的状态,可以证明,他们直接加|x[i] - x[i-1]|即可(如果有其他方案,不符合对 ...

  2. Scalaz(23)- 泛函数据结构: Zipper-游标定位

    外面沙尘滚滚一直向北去了,意识到年关到了,码农们都回乡过年去了,而我却留在这里玩弄“拉链”.不要想歪了,我说的不是裤裆拉链而是scalaz Zipper,一种泛函数据结构游标(cursor).在函数式 ...

  3. AtCoder瞎做第二弹

    ARC 067 F - Yakiniku Restaurants 题意 \(n\) 家饭店,\(m\) 张餐票,第 \(i\) 家和第 \(i+1\) 家饭店之间的距离是 \(A_i\) ,在第 \( ...

  4. 【AtCoder】ARC073

    ARC 073 C - Sentou 直接线段覆盖即可 #include <bits/stdc++.h> #define fi first #define se second #defin ...

  5. Mysql_以案例为基准之查询

    查询数据操作

  6. 2016 ccpc 网络选拔赛 F. Robots

    Robots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  7. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  8. UVA 439 Knight Moves

      // 题意:输入标准国际象棋棋盘上的两个格子,求马最少需要多少步从起点跳到终点 BFS求最短路: bfs并维护距离状态cnt, vis记录是否访问过 #include<cstdio> ...

  9. POJ 2243 Knight Moves

    Knight Moves Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13222   Accepted: 7418 Des ...

随机推荐

  1. Grunt环境搭建及使用

    jQuery在使用grunt,bootstrap在使用grunt,百度UEditor在使用grunt,你没有理由不学.不用! 1. 前言 各位web前端开发人员,如果你现在还不知道grunt或者听说过 ...

  2. codeforce 杀题计划

    先尽量做Div 1 A B 想做难题时做C 全天学竞赛时每天至少两道Div2 (算法数据结构没学的先过,题面很长的......也先过  我的英语啊...)

  3. ORA-01012:not logged on的解决办法

    conn / as sysdba 报错ORA-01012: not logged on 发生原因:关闭数据库是shutdown 后面没有接关闭参数中的任何一个. nomal ————- —-所有连接都 ...

  4. C与C艹的内存管理方式

    C 内存开辟出的空间一般可以分成:代码段,数据段(初始化的数据段, 为初始化的数据段BSS),堆,栈 代码段:保存程序文本,指令指针EIP就是指向代码段,可读可执行不可写 数据段:保存初始化的全局变量 ...

  5. 7 C#变量-把你想要的东西存在C#程序里边

    现在你已经可以用Console.WriteLine("")在dos窗口里打印一些东西出来,而且你还会使用c#进行数学运算.接下来的一步你要开始学习使用变量了.用c#编程,变量就是一 ...

  6. Spring Boot (26) RabbitMQ延迟队列

    延迟消息就是指当消息被发送以后,并不想让消费者立即拿到消息,而是等待指定时间后,消费者才拿到这个消息进行消费. 延迟队列 订单业务: 在电商/点餐中,都有下单后30分钟内没有付款,就自动取消订单. 短 ...

  7. Less——less基本使用

    基本概况 Less 是一门 CSS 预处理语言,它扩充了 CSS 语言,增加了诸如变量.混合(mixin).函数等功能,让 CSS 更易维护.方便制作主题.扩充.Less 可以运行在 Node.浏览器 ...

  8. MyEclipse快捷键 (精简)

    在调试程序的时候,我们经常需要注释一些代码,在用Myeclipse编程时,就可以用 Ctrl+/ 为选中的一段代码加上以 // 打头的注释:当需要恢复代码功能的时候,又可以用Ctrl+/ 去掉注释.这 ...

  9. CSS居中布局方案

    基本结构 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...

  10. Spring学习_day02_AOP,AspectJ,JdbcTemplate

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! Spring_day02 一.AOP面向切面编程 1. ...