A.Two Rival Students

题目:两个竞争的学生##

链接:(两个竞争的对手)[https://codeforces.com/contest/1257/problem/A]

题意:有n个学生排成一行。其中有两个竞争的学生。第一个学生在位置a,第二个学生在位置b,位置从左往右从1到n编号。

你的目的是在经过x次交换后,他们之间的距离最大。(每次交换都是交换相邻的两个学生)

分析:
1.答案是不可能大于n - 1的
2.两者之间的距离可以通过交换增加x,只要答案小于n - 1

因此,取两者最小值就是答案

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;
const int INF = 0x3f3f3f3f;
int main()
{
	int t;
	scanf("%d", &t);
	int n, x, a, b;
	while (t--)
	{
		scanf("%d%d%d%d", &n, &x, &a, &b);

		int ans = INF;
		ans = min(n - 1, abs(a - b) + x);
		printf("%d
", ans);
	}

	return 0;
}
原文地址:https://www.cnblogs.com/pixel-Teee/p/11961265.html