1552B.md (3844B)
1 Source: https://codeforces.com/problemset/problem/1552/B 2 3 B. Running for Gold 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 The Olympic Games have just started and Federico is eager to watch the marathon race. 22 23 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$). 24 25 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} \lt r_{y,j}$ for at least $3$ distinct values of $j$. 26 27 Federico believes that an athlete is likely to get the gold medal at the Olympics if he is superior to all other athletes. 28 29 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. 30 31 Input 32 33 The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. Then $t$ test cases follow. 34 35 The first line of each test case contains a single integer $n$ ($1\le n\le 50\,000$) — the number of athletes. 36 37 Then $n$ lines follow, each describing the ranking positions of one athlete. 38 39 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\le r_{i,j}\le 50\,000$) — 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\le j\le 5$, the $n$ values $r_{1,j},\, r_{2, j},\, \dots,\, r_{n, j}$ are distinct. 40 41 It is guaranteed that the sum of $n$ over all test cases does not exceed $50\,000$. 42 43 Output 44 45 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. 46 47 Example 48 49 Input 50 51 ``` 52 4 53 1 54 50000 1 50000 50000 50000 55 3 56 10 10 20 30 30 57 20 20 30 10 10 58 30 30 10 20 20 59 3 60 1 1 1 1 1 61 2 2 2 2 2 62 3 3 3 3 3 63 6 64 9 5 3 7 1 65 7 4 1 6 8 66 5 6 7 3 2 67 6 7 8 8 6 68 4 2 2 4 5 69 8 3 6 9 4 70 ``` 71 72 Output 73 74 ``` 75 1 76 -1 77 1 78 5 79 ``` 80 81 Note 82 83 Explanation of the first test case: There is only one athlete, therefore he is superior to everyone else (since there is no one else), and thus he is likely to get the gold medal. 84 85 Explanation of the second test case: There are $n=3$ athletes. 86 87 * Athlete $1$ is superior to athlete $2$. Indeed athlete $1$ ranks better than athlete $2$ in the marathons $1$, $2$ and $3$. 88 * Athlete $2$ is superior to athlete $3$. Indeed athlete $2$ ranks better than athlete $3$ in the marathons $1$, $2$, $4$ and $5$. 89 * Athlete $3$ is superior to athlete $1$. Indeed athlete $3$ ranks better than athlete $1$ in the marathons $3$, $4$ and $5$. 90 91 Explanation of the third test case: There are $n=3$ athletes. 92 93 * Athlete $1$ is superior to athletes $2$ and $3$. Since he is superior to all other athletes, he is likely to get the gold medal. 94 * Athlete $2$ is superior to athlete $3$. 95 * Athlete $3$ is not superior to any other athlete. 96 97 Explanation of the fourth test case: There are $n=6$ athletes. 98 99 * Athlete $1$ is superior to athletes $3$, $4$, $6$. 100 * Athlete $2$ is superior to athletes $1$, $4$, $6$. 101 * Athlete $3$ is superior to athletes $2$, $4$, $6$. 102 * Athlete $4$ is not superior to any other athlete. 103 * Athlete $5$ is superior to athletes $1$, $2$, $3$, $4$, $6$. Since he is superior to all other athletes, he is likely to get the gold medal. 104 * Athlete $6$ is only superior to athlete $4$.