一道贪心的题目。
贪心策略
-
将城市位置排序,如果 Alice 出发的位置比 Bob 的出发位置在数轴中靠后,就把他们两个的位置换一下。因为答案不关注顺序。
-
让 Alice 游览 ,让 Bob 游览 。
容易发现 Alice 有两种策略,Bob 也是。
Alice 的策略
Alice 的策略分为两种:
- 。
- 。
代码实现:
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 的策略:
- 。
- 。
代码实现:
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
- 注意特判 等于 时是 Bob 一个人走完全程,Alice 不动; 等于 时是 Alice 一个人走完全程,Bob 不动。
- 记得初始化 ,但是 数组无需清空。
- 记得开 long long。
代码
//#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;
}