【Codeforces】659B Qualifying Contest (sort)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CSDN___CSDN/article/details/88012409

http://codeforces.com/problemset/problem/659/B

n个人,m个地区,选出每个地区分数最高的两个人

下面有n行,每一行的第一个数表示姓名,第二个数是地区的序号,第三个数是分数

It is guaranteed that all surnames of all the participants are distinct and at least two people participated from each of the m regions.

如果来自同一地区的,第二个人和第三个人分数相同,则输出 “ ? ”

结构体排序

#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

struct node
{
	string name;
	int id;
	int grade;
};

node N[100000+5];

bool cmp(node a,node b)
{
	if(a.id!=b.id)
		return a.id<b.id;
	else
		return a.grade>b.grade;
}

int main ()
{
	int i,j,n,m;
	cin >> n >> m;
	for(i=0;i<n;i++)
	{
		cin >> N[i].name >> N[i].id >> N[i].grade;
	}
	sort(N,N+n,cmp);
	j = 1; 
	for(i=0;i<n;i++)
	{
		if(N[i].id==j)
		{
			if(N[i+1].id==j)
			{
				if((N[i+2].id==j && N[i+1].grade!=N[i+2].grade)||(N[i+2].id!=j))
				{
					cout << N[i].name << " " << N[i+1].name << endl;
				}
				else
				{
					cout << "?" << endl;
				}
			}
			else
			{
				cout << "?";
			}
			j++;
		}
	}
	return 0;
} 

vector

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

typedef struct node 
{
	string name;
	int grade;
}node;

vector<node> v[100000+5];

bool cmp(node a,node b)
{
	return a.grade > b.grade;
}

int main ()
{
	int n,m,id,i;
	cin >> n >> m;
	for(i=0;i<n;i++)
	{
		node nn;
		cin >> nn.name >> id >> nn.grade;
		v[id].push_back(nn);
	}
	for(i=1;i<=m;i++)
	{
		sort(v[i].begin(),v[i].end(),cmp);
		if(v[i].size()>2 && v[i][1].grade==v[i][2].grade)
			cout << "?" << endl;
		else
			cout << v[i][0].name << " " << v[i][1].name <<  endl;
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/CSDN___CSDN/article/details/88012409
今日推荐