题目

一个显然的\(dp\),设\(dp_{i,j}\)表示其中一个棋子在\(x_i\)点,另一个棋子在\(j\)点的最小花费

显然\(dp_{i,j}\)有两种转移

第一种是把\(x_i\)上的棋子移到\(x_{i+1}\),那么那么就是\(dp_{i+1,j}=\min(dp_{i,j}+|x_{i+1}-x_i|)\)

第二种就是把\(j\)上的棋子移动到\(x_{i+1}\),那么就是\(dp_{i+1,x_i}=\min(dp_{i,j}+|j-x_{i+1}|)\)

这是\(O(nQ)\)的,考虑优化

发现第一种转移形式非常固定,于是直接整体\(dp\)。第一种转移其实就是全局加,我们维护一个加法标记就可以了。

第二种转移都是转移到\(dp_{i+1,x_i}\),绝对值看起来比较讨厌,考虑将绝对值拆开,当\(j\geq x_{i+1}\)时,\(dp_{i+1,x_i}=dp_{i,j}+j-x_{i+1}\);当\(j\leq x_{i+1}\)时,\(dp_{i+1,x_i}=dp_{i,j}-j+x_{i+1}\),于是考虑直接使用线段树来维护\(dp_{i,j}+j\)和\(dp_{i,j}-j\)的最小值,查一下这两个区间就能转移了。

代码

#include<bits/stdc++.h>
#define re register
#define LL long long
inline int read() {
char c=getchar();int x=0;while(c<'0'||c>'9') c=getchar();
while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+c-48,c=getchar();return x;
}
const LL inf=1e15;
const int maxn=2e5+5;
int l[maxn<<2],r[maxn<<2];LL mx[maxn<<2][2],tag;
int pos[maxn],d[maxn],n,A,B,Q;
inline LL min(LL a,LL b) {return a<b?a:b;}
void build(int x,int y,int i) {
l[i]=x,r[i]=y;mx[i][0]=mx[i][1]=inf;
if(x==y) {pos[x]=i;return;}
int mid=x+y>>1;
build(x,mid,i<<1),build(mid+1,y,i<<1|1);
}
void change(int x,LL v,int o) {
x=pos[x];mx[x][o]=min(v,mx[x][o]);x>>=1;
while(x) mx[x][o]=min(mx[x][o],v),x>>=1;
}
LL query(int x,int y,int i,int o) {
if(x<=l[i]&&y>=r[i]) return mx[i][o];
int mid=l[i]+r[i]>>1;LL now=inf;
if(x<=mid) now=min(now,query(x,y,i<<1,o));
if(y>mid) now=min(now,query(x,y,i<<1|1,o));
return now;
}
inline LL ABS(LL x) {return x>=0?x:-x;}
int main() {
n=read(),Q=read(),A=read(),B=read();
for(re int i=1;i<=Q;i++) d[i]=read();
build(1,n,1);
change(A,ABS(d[1]-B)+A,0);change(A,ABS(d[1]-B)-A,1);
change(B,ABS(d[1]-A)+B,0);change(B,ABS(d[1]-A)-B,1);
for(re int i=2;i<=Q;i++) {
LL x=query(d[i],n,1,0)-d[i],y=query(1,d[i],1,1)+d[i];
x=min(x,y);
LL a=min(mx[pos[d[i-1]]][0]-d[i-1],mx[pos[d[i-1]]][1]+d[i-1]);
if(x<a+ABS(d[i]-d[i-1])) {
x+=tag;tag+=ABS(d[i]-d[i-1]);x-=tag;
change(d[i-1],x+d[i-1],0),change(d[i-1],x-d[i-1],1);
}
else tag+=ABS(d[i]-d[i-1]);
}
LL ans=inf;
for(re int i=1;i<=n;i++) ans=min(ans,mx[pos[i]][1]+i);
printf("%lld\n",ans+tag);
return 0;
}

【ARC073F】Many Moves的更多相关文章

  1. 【arc073f】Many Moves(动态规划,线段树)

    [arc073f]Many Moves(动态规划,线段树) 题面 atcoder 洛谷 题解 设\(f[i][j]\)表示第一个棋子在\(i\),第二个棋子在\(j\)的最小移动代价. 发现在一次移动 ...

  2. 【bfs】Knight Moves

    [题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...

  3. 【arc073D】Many Moves

    Portal -->arc073D Description ​ 有\(n\)个格子,编号从左到右为\(1\sim n\),一开始有两个棋子,位置给定,接下来要依次进行\(Q\)次操作,第\(i\ ...

  4. 【LeetCode】数学(共106题)

    [2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...

  5. 【习题 6-4 UVA-439】Knight Moves

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] bfs模板题 [代码] /* 1.Shoud it use long long ? 2.Have you ever test sev ...

  6. 【广搜】Knight Moves

    题目描述 Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights fr ...

  7. 1450:【例 3】Knight Moves

    1450:[例 3]Knight Moves  题解 这道题可以用双向宽度搜索优化(总介绍在  BFS ) 给定了起始状态和结束状态,求最少步数,显然是用BFS,为了节省时间,选择双向BFS. 双向B ...

  8. 【模拟】Codeforces 710A King Moves

    题目链接: http://codeforces.com/problemset/problem/710/A 题目大意: 国际象棋标准8X8棋盘,国王能往周围8个方向走.输入国王的位置,输出当前国王能往几 ...

  9. 带给你灵感:30个超棒的 SVG 动画展示【上篇】

    前端开发人员和设计师一般使用 CSS 来创建 HTML 元素动画.然而,由于 HTML 在创建图案,形状,和其他方面的局限性,它们自然的转向了 SVG,它提供了更多更有趣的能力.借助SVG,我们有更多 ...

随机推荐

  1. ()centos7 安装python36

    centos7 默认安装 python2.7 1.先安装python36和对应pip yum install python-pip #安装python2的pip yum install python3 ...

  2. HDU 1700 Points on Cycle (坐标旋转)

    题目链接:HDU 1700 Problem Description There is a cycle with its center on the origin. Now give you a poi ...

  3. 动态方法调用秘密武器 —— invokedynamic 指令解读 - MethodHandle

    原文:https://juejin.im/book/5c25811a6fb9a049ec6b23ee/section/5ccc66dd518825403b5975fb import java.lang ...

  4. Python CookBook(self report)

    Python CookBook 中文版:https://python3-cookbook.readthedocs.io/zh_CN/latest/copyright.html 英文版:https:// ...

  5. 记录百度编辑器bug(在编辑框输入光标到达页面最底部时,功能区块会悬浮在页面最顶部并且与编辑框分离)

    解决方案: //加入这段CSS#edui1_toolbarbox { position: relative !important; }

  6. mysql数据库中某字段一部分乱码

    笔者问题:mysql表(表中数据就是乱码,可能是插入时编码问题,这个问题以后解决)导出excel时数据中有乱码(但是在页面上查看是正常的),我们希望能导出一份没有中文乱码的excel 根据热力站中一次 ...

  7. Linux特殊位SUID、SGID、SBIT

    Linux特殊位SUID.SGID.SBIT 前言 Linux中的文件权限一般有x.w.r,在某个情况下有需要用到s.t,即特殊位. 进程运行时能够访问哪些资源或文件,不取决于进程文件的属主属组,而是 ...

  8. 2018-11-24-C#-7.0

    title author date CreateTime categories C# 7.0 lindexi 2018-11-24 16:32:58 +0800 2018-2-13 17:23:3 + ...

  9. BeanUtils.copyProperties用法

    spring的BeanUtils.copyProperties用法 原创 2010年06月03日 13:43:00 标签: spring / struts / 数据库 / 工具 / action 一. ...

  10. Effective C++之条款2:尽量以const enum inline替换 #define

    本文的标题也可以改成“用编译器替换预处理器”: const double AspectRatio = 1.653; //最好使用上述代码替换下述代码: #define ASPECT_RATIO 1.6 ...