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();
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
2015-11-14 Codeforces Round #282 (Div. 1) A. Treasure 水题
2015-11-14 hdu 5565 Clarke and baton 二分
2015-11-14 hdu 5563 Clarke and five-pointed star 水题