跳到正文

题解

题解:P15290 [MCO 2023] Two Pointers (easy version)

ZBH2015约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年11月24日
收录
2026年11月29日
更新
2026年11月29日
篇幅
约 8 分钟
题目
P15290

版本

  1. v1当前

一道贪心的题目。

贪心策略

  • 将城市位置排序,如果 Alice 出发的位置比 Bob 的出发位置在数轴中靠后,就把他们两个的位置换一下。因为答案不关注顺序。

  • 让 Alice 游览 [t1,ti][t_1, t_i],让 Bob 游览 [ti+1,tn][t_{i+1}, t_n]。

容易发现 Alice 有两种策略,Bob 也是。

Alice 的策略

Alice 的策略分为两种:

  • A→t1→tiA\rightarrow t_1\rightarrow t_i。
  • A→ti→t1A\rightarrow t_i\rightarrow t_1。

代码实现:

C++
ll ato1=abs(a-t[1]);
ll ito1=t[i]-t[1];
ll a1=ito1+ato1;
ll a2=ito1+abs(a-t[i]);

Bob 的策略

同 Alice 的策略:

  • B→tn→ti+1B\rightarrow t_n\rightarrow t_{i+1}。
  • B→ti+1→tnB\rightarrow t_{i+1}\rightarrow t_n。

代码实现:

C++
ll bton=abs(b-t[n]);
ll iton=t[n]-t[i+1];
ll b1=iton+bton;
ll b2=iton+abs(b-t[i+1]);

实现方法

按照贪心策略模拟即可。

Tips

  • 注意特判 ii 等于 00 时是 Bob 一个人走完全程,Alice 不动;ii 等于 nn 时是 Alice 一个人走完全程,Bob 不动。
  • 记得初始化 ansans,但是 tt 数组无需清空。
  • 记得开 long long。

代码

C++
//#pragma GCC optimize("Ofast,unroll-loops")
#pragma g++ optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma")
#include<bits/stdc++.h>
using namespace std;
#define F(i,j,k,l) for(int i=(j);i<=(k);i+=(l))
#define f(i,j,k,l) for(int i=(j);i>=(k);i-=(l))
// #ifdef __unix__
// #define gc getchar_unlocked
// #define pc putchar_unlocked
// #else
// #define gc _getchar_nolock
// #define pc _putchar_nolock
// #endif
#define W(i) while(i)
#define A auto
#define ot float
#define FO(x,a) for(int x:a)
#define Fo(x,a) for(A x:a)
#define el "\n"
#define ull unsigned long long
#define ll long long
#define in __int128
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define dle double
#define ld long double
#define imax INT_MAX
#define imin INT_MIN
#define llmax LONG_LONG_MAX
#define llmin LONG_LONG_MIN
#define debug(x) cerr<<#x<<" "<<x<<"\n";
const int N=2e5+10,M=1<<20;
char buf[M],*p1=buf,*p2=buf,out[M],*p3=out;
//#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,M,stdin)),*p1++)
#define gc() (p1==p2?(p2=(p1=buf)+fread(buf,1,M,stdin),p1==p2?EOF:*p1++):*p1++)
//#define flush() do{fwrite(out,1,p3-out,stdout);p3=out;}while(0)
//#define pc(c) do{if(p3==(out+M)) flush();*p3++=(c);}while(0)
#define flush() (fwrite(out,1,p3-out,stdout),p3=out)
#define pc(c) (p3==(out+M)?(flush(),*p3++=(c)):*p3++=(c))
inline long long read(){
    long long x=0,f=1;
    char ch=gc();
    W(!isdigit(ch)){
        if(ch=='-') f=-1;
        ch=gc();
    }
    W(isdigit(ch)){
        x=x*10+(ch-'0');
        ch=gc();
    }
    return x*f;
}
inline string rsline(){
    string s;
    char ch=gc();
    W(ch!='\n'&&ch!='\r'&&ch!=EOF) s+=ch,ch=gc();
    return s;
}
inline string rs(){
    string s;
    char ch=gc();
    W(ch==' '||ch=='\n'||ch=='\r'||ch=='\t') ch=gc();
    W(ch!=' '&&ch!='\n'&&ch!='\r'&&ch!='\t'&&ch!=EOF) s+=ch,ch=gc();
    return s;
}
inline void write(long long x){
    if(x<0){
        pc('-');
        x=-x;
    }
    if(x==0){
        pc('0');
        return;
    }
    char s[25];
    int len=0;
    W(x){
        s[len++]=char('0'+x%10);
        x/=10;
    }
    W(len) pc(s[--len]);
    return;
}
inline void ws(string &s){
    for(auto x:s) pc(x);
    return;
}
ll n,a,b,t[N];
ll ans;
inline void pre(){
    ans=1e18;
}
inline void input(){
    n=read(),a=read(),b=read();
    if(a>b) swap(a,b);
    F(i,1,n,1) t[i]=read();
}
inline void output(){
    sort(t+1,t+n+1);
    ll nto1=t[n]-t[1];
    ll ato1=abs(a-t[1]);
    ll bton=abs(b-t[n]);
    F(i,0,n,1){
        ll ito1=t[i]-t[1];
        ll iton=t[n]-t[i+1];
        ll a1=ito1+ato1;
        ll a2=ito1+abs(a-t[i]);
        ll b1=iton+bton;
        ll b2=iton+abs(b-t[i+1]);
        ll suma=min(a1,a2);
        ll sumb=min(b1,b2);
        if(i==0) ans=min(sumb,ans);
        else if(i==n) ans=min(suma,ans);
        else ans=min(suma+sumb,ans);
    }
    write(ans);
    pc('\n');
}
inline void solve(){
    pre();
    input();
    output();
    return;
}
int main(){
    int _=1;_=read();
    while(_--) solve();
    flush();
    return 0;
}

AC记录

原文