The Olympic Games have just started and Federico is eager to watch the marathon race.
There will be n athletes, numbered from 1 to n , competing in the marathon, and all of them have taken part in 5 important marathons, numbered from 1 to 5 , in the past. For each 1\le i\le n and 1\le j\le 5 , Federico remembers that athlete i ranked r_{i,j} -th in marathon j (e.g., r_{2,4}=3 means that athlete 2 was third in marathon 4 ).
Federico considers athlete x superior to athlete y if athlete x ranked better than athlete y in at least 3 past marathons, i.e., r_{x,j}<r_{y,j}3 distinct values of j .
Federico believes that an athlete is likely to get the gold medal at the Olympics if he is superior to all other athletes.
Find any athlete who is likely to get the gold medal (that is, an athlete who is superior to all other athletes), or determine that there is no such athlete.
The first line contains a single integer t (1≤t≤1000) — the number of test cases. Then t test cases follow.
The first line of each test case contains a single integer n (1≤n≤50000) — the number of athletes.
Then n lines follow, each describing the ranking positions of one athlete.
The i-th of these lines contains the 5 integers r_{i,1},r_{i,2},r_{i,3},r_{i,4},r_{i,5} (1≤ri,j≤50000) — the ranking positions of athlete ii in the past 5 marathons. It is guaranteed that, in each of the 5 past marathons, the n athletes have distinct ranking positions, i.e., for each 1≤j≤5, the nn values r_{1,j},r_{2,j},…,r_{n,j} are distinct.
It is guaranteed that the sum of n over all test cases does not exceed 50000.
For each test case, print a single integer — the number of an athlete who is likely to get the gold medal (that is, an athlete who is superior to all other athletes). If there are no such athletes, print -1 . If there is more than such one athlete, print any of them.
4
1
50000 1 50000 50000 50000
3
10 10 20 30 30
20 20 30 10 10
30 30 10 20 20
3
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
6
9 5 3 7 1
7 4 1 6 8
5 6 7 3 2
6 7 8 8 6
4 2 2 4 5
8 3 6 9 4
1
-1
1
5
首先,要判断是否会有运动员获得奖牌,只需要判断最有可能获得奖牌的运动员是否满足条件即可。
然后,我们需要找到所有运动员获得奖牌可能性的顺序。显然,A 在至少 3 场比赛中优于 B,B 在至少 3 场比赛中优于 C,A 并不一定在至少三场比赛中优于 C,所以胜负关系并不具有传递性,自然的想到以两人之间的胜负关系进行排序,最后再判断最有可能获胜的运动员是否满足条件即可。
以两人之间的胜负关系的排序可以用快速排序进行优化,时间复杂度 O(n \log n)
#include<bits/stdc++.h>
using namespace std;
int t,n;
int race[50001][6];
int racer[50001];
bool cmp(int x,int y){
int sum=0;
for(int i=1;i<=5;i++){
if(race[x][i]<race[y][i])sum++;
}
if(sum>=3)return true;
else return false;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=1;i<=n;i++){
racer[i]=i;
for(int j=1;j<=5;j++){
scanf("%d",&race[i][j]);
}
}
sort(racer+1,racer+1+n,cmp);
for(int i=2;i<=n;i++){
if(cmp(racer[1],racer[i])==false){
printf("-1\n");
goto s;
}
}
printf("%d\n",racer[1]);
s:;
}
}
最后修改:2021 年 08 月 04 日 11 : 32 AM
© 允许规范转载