qscqesze
Published on 2017-09-02 11:31 in 暂未分类 with qscqesze

Educational Codeforces Round 76 (Rated for Div. 2) A. Two Rival Students 水题

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();
}
posted @ 2019-11-14 17:46  qscqesze  阅读(119)  评论(0编辑  收藏