前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【题解】Running for Gold

【题解】Running for Gold

作者头像
MikeC
发布2022-09-21 15:00:56
4230
发布2022-09-21 15:00:56
举报
文章被收录于专栏:MikeC's BlogMikeC's Blog

题目描述

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.

输入输出样例

输入 #1

代码语言:javascript
复制
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

代码语言:javascript
复制
1
-1
1
5

分析

首先,要判断是否会有运动员获得奖牌,只需要判断最有可能获得奖牌的运动员是否满足条件即可。

然后,我们需要找到所有运动员获得奖牌可能性的顺序。显然,A 在至少 3 场比赛中优于 BB 在至少 3 场比赛中优于 CA 并不一定在至少三场比赛中优于 C,所以胜负关系并不具有传递性,自然的想到以两人之间的胜负关系进行排序,最后再判断最有可能获胜的运动员是否满足条件即可。

以两人之间的胜负关系的排序可以用快速排序进行优化,时间复杂度 O(n \log n)

代码

代码语言:javascript
复制
#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

© 允许规范转载

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021 年 08 月,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述
  • 输入格式
  • 输出格式
  • 输入输出样例
    • 输入 #1
      • 输出 #1
      • 分析
      • 代码
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档