题目描述:
You are the gym teacher in the school.
There are n students in the row. And there are two rivalling students among them. The first one is in position a, the second in position b. Positions are numbered from 1 to n from left to right.
Since they are rivals, you want to maximize the distance between them. If students are in positions p and s respectively, then distance between them is |p−s|.
You can do the following operation at most x times: choose two adjacent (neighbouring) students and swap them.
Calculate the maximum distance between two rivalling students after at most x swaps.
输入描述:
The first line contains one integer t (1≤t≤100) — the number of test cases.
The only line of each test case contains four integers n, x, a and b (2≤n≤100, 0≤x≤100, 1≤a,b≤n, a≠b) — the number of students in the row, the number of swaps which you can do, and positions of first and second rivaling students respectively.
输出描述:
For each test case print one integer — the maximum distance between two rivaling students which you can obtain.
输入:
3
5 1 3 2
100 33 100 1
6 0 2 3
输出:
2
3
1
题意:
有n个学生站成一排。但其中有两个学生是死对头。第一个学生的位置是a,第二个学生的位置是b。位置的标号从左到右依次为1~n。
作为一名教师, 你的责任是让他们尽可能地远离彼此,永远不要在一起。他们之间距离的计算式子为|a - b|。
你可以交换相邻的两个学生x次。请你计算在交换x次后这两名学生的距离。
题解:
小的往左移,大的往右移
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int t;
int n,x,a,b;
scanf("%d",&t);
while(t--){
scanf("%d%d%d%d",&n,&x,&a,&b);
int minn = min(a,b);
int maxx = max(a,b);
if(x != 0 && minn != 1){
int dis = minn - 1;
if(dis > x){
minn -= x;
x = 0;
}
else{
minn = 1;
x -= dis;
}
}
if(x != 0 && maxx != n){
int dis = n - maxx;
if(dis > x){
maxx += x;
x = 0;
}
else{
maxx = n;
x -= dis;
}
}
int ans = maxx - minn;
printf("%d\n",ans);
}
return 0;
}