Codeforces Round #346 (Div. 2)--B. Qualifying Contest

来源:互联网 发布:进销存数据库表设计 编辑:程序博客网 时间:2024/05/22 16:01
B. Qualifying Contest
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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 0001 ≤ m ≤ 10 000n ≥ 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 1to 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 2Ivanov 1 763Andreev 2 800Petrov 1 595Sidorov 1 790Semenov 2 503
output
Sidorov IvanovAndreev Semenov
input
5 2Ivanov 1 800Andreev 2 763Petrov 1 800Sidorov 1 800Semenov 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 个符合竞赛的参加者来自 Berland 的 m 个不同区域,每个人名字不会重复,


即使字母全相同也区分大小写,每个人有姓名,区域号及成绩(0--800),你需要将 n 


个人中选 2*m 个人组队,一个队只能有 2 人,按成绩择优选取,多个符合条件成绩相


同的情况则无法确定时输出“?”,否则按队编号小到大给出 m 行,名字顺序任意。


思路:直接用个结构体数组存储每个区域成绩最高的前 2 名,如果不能确定组队的人


员则将其标记为0,否则标记为 1。

代码如下:

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;#define size 120000#define inf 0x3f3f3f3fstruct stu{char name[12];int num, sco, flag;}s[size], team[size / 10];bool cmp(stu& a, stu& b){if (a.num == b.num)//号数一致时按分大到小排return a.sco > b.sco;return a.num < b.num;//按号数小到大排}int main(){#ifdef OFFLINEfreopen("t.txt", "r", stdin);#endifint m, n, i, j, k, two;while (~scanf("%d%d", &n, &m)){memset(team, NULL, sizeof(team));for (i = 1; i <= n; i++)scanf("%s%d%d", s[i].name, &s[i].num, &s[i].sco);sort(s + 1, s + n + 1, cmp);k = 1;for (i = 1; i <= m; i++){two = 0;for (j = (i - 1) * 2 + 1; j <= n; j++){if (s[j].num == i&&two < 2){team[k] = s[j];team[k++].flag = 1;two++;}if (two == 2 && s[j + 1].num == i&&s[j + 1].sco == s[j].sco)team[k - 2].flag = 0;//无法确定人选则标记为0if (two == 2)  break;}}for (i = 1; i <= m; i++){if (!team[(i - 1) * 2 + 1].flag){printf("?\n");}else{printf("%s %s\n", team[(i - 1) * 2 + 1].name, team[(i - 1) * 2 + 2].name);}}}return 0;}


0 0
原创粉丝点击