B. Running for Gold
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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≤i≤n and 1≤j≤5, Federico remembers that athlete i ranked ri,j-th in marathon j (e.g., r2,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., rx,j<ry,j for at least 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.
Input
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 ri,1,ri,2,ri,3,ri,4,ri,5 (1≤ri,j≤50000) — the ranking positions of athlete i 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 n values r1,j,r2,j,…,rn,j are distinct.
It is guaranteed that the sum of n over all test cases does not exceed 50000.
Output
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.
Example
input
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
output
1
-1
1
5
/*
时间复杂度:O(n)
首先我们知道,只要比一个运动员差,那么就没有机会得到金牌
先如同打擂台的方式找出可能得到金牌的选手,然后在枚举所有的运动员,
看能否比所有的运动员优秀
*/
#include <iostream>
using namespace std;
const int N = 50010;
int a[N][5];
int main()
{
int T;
cin >> T;
while(T --)
{
int n;
cin >> n;
for(int i = 1;i <= n; i ++)
for(int j = 0; j < 5; j ++) cin >> a[i][j];
int t = 1;
for(int i = 2; i <= n; i ++)
{
int cnt = 0;
for(int j = 0; j < 5; j ++)
{
if(a[i][j] < a[t][j]) cnt ++;
}
if(cnt >= 3) t = i;
}
bool flag = false;
for(int i = 1; i <= n; i ++)
{
if(i == t) continue;
int cnt = 0;
for(int j = 0; j < 5; j ++)
{
if(a[t][j] < a[i][j]) cnt ++;
}
if(cnt < 3)
{
flag = true;
break;
}
}
if(flag) puts("-1");
else cout << t << endl;
}
return 0;
}