链接
B. Different Rules
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Nikolay has only recently started in competitive programming, but already qualified to the finals of one prestigious olympiad. There going to be n participants, one of whom is Nikolay. Like any good olympiad, it consists of two rounds. Tired of the traditional rules, in which the participant who solved the largest number of problems wins, the organizers came up with different rules.
Suppose in the first round participant A took x-th place and in the second round — y-th place. Then the total score of the participant A is sum x+y. The overall place of the participant A is the number of participants (including A) having their total score less than or equal to the total score of A. Note, that some participants may end up having a common overall place. It is also important to note, that in both the first and the second round there were no two participants tying at a common place. In other words, for every i from 1 to n exactly one participant took i-th place in first round and exactly one participant took i-th place in second round.
Right after the end of the Olympiad, Nikolay was informed that he got x-th place in first round and y-th place in the second round. Nikolay doesn’t know the results of other participants, yet he wonders what is the minimum and maximum place he can take, if we consider the most favorable and unfavorable outcome for him. Please help Nikolay to find the answer to this question.
Input
The first line contains an integer t (1≤t≤100) — the number of test cases to solve.
Each of the following t lines contains integers n, x, y (1≤n≤109, 1≤x,y≤n) — the number of participants in the olympiad, the place that Nikolay took in the first round and the place that Nikolay took in the second round.
Output
Print two integers — the minimum and maximum possible overall place Nikolay could take.
Examples
inputCopy
1
5 1 3
outputCopy
1 3
inputCopy
1
6 3 4
outputCopy
2 6
Note
Explanation for the first example:
Suppose there were 5 participants A-E. Let’s denote Nikolay as A. The the most favorable results for Nikolay could look as follows:
However, the results of the Olympiad could also look like this:
题意:你是选手A参加4场比赛,已知前两场比赛的排名,求剩下的2场比赛过后,你可能的最好排名和最差排名。
思路:设第一场第二场排名分别为 x,y ,n为选手总数。
- 求最好成绩:当 x + y <= n 时,可以发现,最好情况总是能排在第一名。
- 当 x + y > n 时,尽量使其他参赛者的成绩在 x + y + 1,成绩不可能到 x + y + 1 的,成绩尽可能的好,可以得到A的最好成绩为 x + y - n + 1, 但是可以发现,x + y - n + 1 可以大于n,不符合实际,所以最好成绩等于 min(x+y-n+1,n)。
- 求最差成绩:应该尽量使其他参赛者的分数与A相同。
- 当 x + y <= n + 1 , 有 1 + (x + y - 1) == x + y, … , (x + 1) + (y - 1) == x + y , (x + 2) + (y - 2) == x + y, … , (x + y - 1) + 1 == x + y . 以此类推,一共有x + y - 1 的情况使其他参赛者分数相同,所以最差成绩为 x + y - 1
- 当 x + y > n + 1 , 我们令y = n ,则 x = x - (n - y) , 这样的成绩作为第一场和第二场的成绩,第一场成绩优于x的,显然A已经无法超越,但可以和劣于x的得同分。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin >> T;
while(T--){
int n,x,y;
cin >> n >> x >> y;
if (x+y <= n){
cout << 1 << " ";
}
else{
cout << min(x+y-n+1,n) << " " ;
}
if (x+y <= n+1){
cout << x+y-1 << endl;
}
else{
x -= n-y;
y = n;
int t = x - 1;
x -= t; y -= t; n -= t;
cout << x + y - 1 + t << endl;
}
}
return 0;
}
其他:
#include <cstdio>
#include <algorithm>
using namespace std;
int main(){
int t,n,x,y,good,bad;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&x,&y);
bad=min(n,x+y-1);
if(x+y<=n)good=1;
else good=min(n,x+y-n+1);//x+y>=n+1
printf("%d %d\n",good,bad);
}
return 0;
}
上面代码链接:https://blog.csdn.net/mrcrack/article/details/104486588/