F - Many Moves

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+10;
const int N=2e5+10;
using namespace std;
int id(int l,int r){return l+r|l!=r;}
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p%mod;p=p*p%mod;q>>=1;}return f;}
int n,m,k,t,q,a,b;
ll tag[maxn<<2],mi[maxn<<2],mi1[maxn<<2],mi2[maxn<<2];
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]=0;
}
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]=0;
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        mi[rt]=mi1[rt]=mi2[rt]=1e18;
        tag[rt]=0;
        return;
    }
    int mid=l+r>>1;
    build(l,mid,ls);
    build(mid+1,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>>1;
    if(tag[rt])pdw(rt);
    if(R<=mid)add(L,R,v,l,mid,ls);
    else if(L>mid)add(L,R,v,mid+1,r,rs);
    else
    {
        add(L,mid,v,l,mid,ls);
        add(mid+1,R,v,mid+1,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>>1;
    if(tag[rt])pdw(rt);
    if(pos<=mid)upd(pos,v,l,mid,ls);
    else upd(pos,v,mid+1,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>>1;
    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+1,r,rs,mi);
    else return min(gao(L,mid,l,mid,ls,mi),gao(mid+1,R,mid+1,r,rs,mi));
}
int main()
{
    int i,j;
    scanf("%d%d%d%d",&n,&q,&a,&b);
    build(1,n,1);
    upd(b,0,1,n,1);
    int pre=a;
    rep(i,1,q)
    {
        int x;
        scanf("%d",&x);
        ll cost1=gao(1,x,1,n,1,mi1)+x;
        ll cost2=gao(x,n,1,n,1,mi2)-x;
        ll now=min(cost1,cost2);
        add(1,n,abs(x-pre),1,n,1);
        upd(pre,now,1,n,1);
        pre=x;
    }
    printf("%lld\n",gao(1,n,1,n,1,mi));
    return 0;
}
时间: 2024-10-26 04:47:28

F - Many Moves的相关文章

过分过分进货价获国家

http://f.dangdang.com/group/24554/3491082/http://f.dangdang.com/group/24554/3491087/http://f.dangdang.com/group/24554/3491094/http://f.dangdang.com/group/24554/3491099/http://f.dangdang.com/group/24554/3491105/http://f.dangdang.com/group/24554/349111

我们找个地方看好戏

http://v.qq.com/page/f/y/4/m041433ssun.html http://v.qq.com/page/f/y/4/m041433ssun.html http://v.qq.com/page/f/y/4/m04143o3lhg.html http://v.qq.com/page/f/y/4/m04144675h3.html http://v.qq.com/page/f/y/4/m04144k1k1j.html http://v.qq.com/page/f/y/4/m04

codeforces 710A A. King Moves(水题)

题目链接: A. King Moves 题意: 给出king的位置,问有几个可移动的位置; 思路: 水题,没有思路; AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> #include &l

暑假练习赛 003 F Mishka and trip

F - Mishka and trip Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description Input Output Sample Input Sample Output Hint Description Peter Parker wants to play a g

Sicily Knight Moves(BFS)

1000. Knight Moves                       Time Limit: 1sec    Memory Limit:32MB Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square

HDFS F ile System Shell Guide

Overview appendToFile cat checksum chgrp chmod chown copyFromLocal copyToLocal count cp createSnapshot deleteSnapshot df du dus expunge find get getfacl getfattr getmerge help ls lsr mkdir moveFromLocal moveToLocal mv put renameSnapshot rm rmdir rmr

HDOJ 1372 Knight Moves【BFS】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7482    Accepted Submission(s): 4481 Problem Description A friend of you is doin

HDU 1372 Knight Moves (bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10372    Accepted Submission(s): 6105 Problem Description A friend of you is doin

16级第三周寒假作业F题

Sliding Window TimeLimit:12000MS  MemoryLimit:65536K 64-bit integer IO format:%lld Problem Description Case Time Limit: 5000MS An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the arr