Codeforces - Global Round 15 - B. Running for Gold

Codeforces Global Round 15

B. Running for Gold

time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard 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

思路

一开始做本题时,一直都是Time limit exceeded, 因为我一开始是想用 i ,j 来二重循环遍历找到一个athlete,让他强于其余的人,这样做的时间复杂度太高了。
所以,经过分析我们可以发现:

  1. 用 j 标记第一athlete,先遍历一遍 i,比较 athlete i 和 athlete j,若athlete i 强于 athlete j,则 j = i;这样做,是因为只要athlete会输于某一个athlete,那这个athlete肯定不能拿金,所以可以直接把他排除。
  2. 经过遍历一次后,我们可以得到一个athlete j ,但是这个athlete j 还是不一定能拿金,应为他必须要强于所有人,所以我们可以在遍历一遍i,若存在athlete i 强于 athlete j,则无人可以拿金(-1),否则athlete j 就可以拿金。

C++代码

#include<iostream>
using namespace std;
struct people{
	int r[5];
}p[50010];
//It is guaranteed that the sum of n over all test cases does not exceed 50000.
bool compare(people p1,people p2){
	int num=0;
	for(int i=0;i<5;i++)
		if(p1.r[i] < p2.r[i]) num++;
	if(num>=3) return true;
	else return false;
}

int main(){
	int n,m,k=0;
	cin >> n;
	int i,j;
	while(n--){
		scanf("%d",&m);
		for(i=k;i<k+m;i++)
			for(j=0;j<5;j++)
				scanf("%d",&p[i].r[j]);
		int dp=0;
		j=k;
		for(int i=k+1;i<k+m;i++)
			if(compare(p[i],p[j])) j=i;
		for(int i=k;i<k+m;i++)
			if(i!=j && compare(p[i],p[j])){
				dp=1;
				break;
			}
		if(dp==0) cout << j-k+1 << endl;
		else if (dp==1)   cout << -1 << endl;
		k = k+m;			
	}		
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值