A. Two Rival Students
There are 𝑛 students in the row. And there are two rivalling students among them. The first one is in position 𝑎, the second in position 𝑏. Positions are numbered from 1 to 𝑛 from left to right.
Since they are rivals, you want to maximize the distance between them. If students are in positions 𝑝 and 𝑠 respectively, then distance between them is |𝑝−𝑠|.
You can do the following operation at most 𝑥 times: choose two adjacent (neighbouring) students and swap them.
Calculate the maximum distance between two rivalling students after at most 𝑥 swaps.
Input
The first line contains one integer 𝑡 (1≤𝑡≤100) — the number of test cases.
The only line of each test case contains four integers 𝑛, 𝑥, 𝑎 and 𝑏 (2≤𝑛≤100, 0≤𝑥≤100, 1≤𝑎,𝑏≤𝑛, 𝑎≠𝑏) — the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively.
Output
For each test case print one integer — the maximum distance between two rivaling students which you can obtain.
Example
input
3
5 1 3 2
100 33 100 1
6 0 2 3
output
2
99
1
Note
In the first test case you can swap students in positions 3 and 4. And then the distance between the rivals is equal to |4−2|=2.
In the second test case you don't have to swap students.
In the third test case you can't swap students.
題意
現在有n個東西排列成一行,a在第a個位置,b在第b個位置,現在每次操作可以使得一個人和周圍的人交換位置,問你在操作最多x次的情況下,最多能夠使得a和b的距離最遠是多少
題解
每次交換一次,肯定可以使得距離加一,那麼答案就是要麼就最遠,要麼就目前的距離+x即可。
代碼
#include<bits/stdc++.h>
using namespace std;
void solve(){
int n,x,a,b;
cin>>n>>x>>a>>b;
if(a>b)swap(a,b);
cout<<min(b-a+x,n-1)<<endl;
}
int main(){
int t;cin>>t;
while(t--)solve();
}