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(); }