B. Qualifying Contest
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Very soon Berland will hold a School Team Programming Olympiad. From each of the m Berland regions a team of two people is invited to participate in the olympiad. The qualifying contest to form teams was held and it was attended by n Berland students. There were at least two schoolboys participating from each of the m regions of Berland. The result of each of the participants of the qualifying competition is an integer score from 0 to 800 inclusive.
The team of each region is formed from two such members of the qualifying competition of the region, that none of them can be replaced by a schoolboy of the same region, not included in the team and who received a greater number of points. There may be a situation where a team of some region can not be formed uniquely, that is, there is more than one school team that meets the properties described above. In this case, the region needs to undertake an additional contest. The two teams in the region are considered to be different if there is at least one schoolboy who is included in one team and is not included in the other team. It is guaranteed that for each region at least two its representatives participated in the qualifying contest.
Your task is, given the results of the qualifying competition, to identify the team from each region, or to announce that in this region its formation requires additional contests.
Input
The first line of the input contains two integers n and m (2?≤?n?≤?100?000, 1?≤?m?≤?10?000, n?≥?2m) — the number of participants of the qualifying contest and the number of regions in Berland.
Next n lines contain the description of the participants of the qualifying contest in the following format: Surname (a string of length from 1 to 10 characters and consisting of large and small English letters), region number (integer from 1 to m) and the number of points scored by the participant (integer from 0 to 800, inclusive).
It is guaranteed that all surnames of all the participants are distinct and at least two people participated from each of the m regions. The surnames that only differ in letter cases, should be considered distinct.
Output
Print m lines. On the i-th line print the team of the i-th region — the surnames of the two team members in an arbitrary order, or a single character “?” (without the quotes) if you need to spend further qualifying contests in the region.
Examples
input
5 2
Ivanov 1 763
Andreev 2 800
Petrov 1 595
Sidorov 1 790
Semenov 2 503
output
Sidorov Ivanov
Andreev Semenov
input
5 2
Ivanov 1 800
Andreev 2 763
Petrov 1 800
Sidorov 1 800
Semenov 2 503
output
?
Andreev Semenov
Note
In the first sample region teams are uniquely determined.
In the second sample the team from region 2 is uniquely determined and the team from region 1 can have three teams: “Petrov”-“Sidorov”, “Ivanov”-“Sidorov”, “Ivanov” -“Petrov”, so it is impossible to determine a team uniquely.
题目大意:
就是给了n个名字,m个地方,然后我们需要每个地方中选取两个最大的分数,输出的是两个最大的分数的名字,如果第二和第三的分数是一样的,我们就输出’?’
解题思路:
这个题其实是一个水题,然后我们就是排一下序就行了,先把m个区域排好序,然后将分数从大到小排好序,然后就是判断一下就行了。注意的是可能不到2个人,那么也是输出’?’,还有一个小技巧就是把arr[n].id==-1,因为我们每次判断的是当前的id与它的前一个是不是相同,所以最后要将n那个赋为-1,有两种方法都可以参考一下:
第一个:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int MAXN = 1e5+5;
struct sa
{
int id, sc;
char name[105];
} arr[MAXN];
bool cmp(sa a, sa b)
{
if(a.id == b.id)
return a.sc > b.sc;
return a.id < b.id;
}
int main()
{
int n, m;
while(cin>>n>>m)
{
for(int i=0; i<n; i++)
{
cin>>arr[i].name>>arr[i].id>>arr[i].sc;
}
sort(arr, arr+n, cmp);
int d = 0;
arr[n].id = -1;///小技巧
for(int i=1; i<=n; i++)
{
if(arr[i].id != arr[i-1].id)
{
if(i-d < 2)
{
puts("?");
d = i;
continue;
}
if(i-d == 2)///需要注意这里
{
cout<<arr[d].name<<" "<<arr[d+1].name<<endl;
d = i;
continue;
}
if(arr[d+1].sc != arr[d+2].sc)
{
cout<<arr[d].name<<" "<<arr[d+1].name<<endl;
}
else
{
puts("?");
}
d = i;
}
}
}
return 0;
}
第二个是用vector数组写的:
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int MAXN = 1e5+5;
struct sa
{
int id, sc;
char name[105];
} arr[MAXN];
bool cmp(sa a, sa b)
{
if(a.id == b.id)
return a.sc > b.sc;
return a.id < b.id;
}
vector <sa> vec[MAXN];
int main()
{
int n, m;
while(cin>>n>>m)
{
for(int i=0; i<n; i++)
{
cin>>arr[i].name>>arr[i].id>>arr[i].sc;
vec[arr[i].id].push_back(arr[i]);
}
for(int i=1; i<=m; i++)
{
sort(vec[i].begin(), vec[i].end(), cmp);
if(vec[i].size() == 1)
{
puts("?");
}
else if(vec[i].size() == 2)
cout<<vec[i][0].name<<" "<<vec[i][1].name<<endl;
else
{
if(vec[i][1].sc == vec[i][2].sc)
puts("?");
else
cout<<vec[i][0].name<<" "<<vec[i][1].name<<endl;
}
}
}
return 0;
}