我们可以分类讨论。如果位置靠左的能一直往左换就可以换完所有次数位置靠右的就不用移动了,否则就往右移动。但可能都移到两边了次数还没用完,所以再判断一次就好了。
#include<bits/stdc++.h>
using namespace std;
int t,n,x,a,b;
int main(){
cin>>t;
for(int k=1;k<=t;k++){
cin>>n>>x>>a>>b;
if(a>b)swap(a,b);//排序
if(x<a)cout<<b-a+x<<endl;//判断
else{
if(x-a<n-b)cout<<b+x-a<<endl;
else cout<<n-1<<endl;
}
}
return 0;
}