Codeforces 659 B. Qualifying Contest (Problem of structural sorting)

Source: Internet
Author: User

Portal
B. Qualifying Contest
Time limit per test1 second
Memory limit per test256 megabytes
Inputstandard input
Outputstandard output
Very soon Berland would hold a School Team programming Olympiad. From each of the M Berland regions a team of the people are invited to participate in the Olympiad. The qualifying contest to form teams is held and it is attended by N Berland students. There were at least, schoolboys participating from each of the M regions of Berland. The result of each of the participants of the qualifying competition is a integer score from 0 to inclusive.

The team of each are formed from and such members of the qualifying competition of the region, that's 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 was a situation where a team of some region can isn't being formed uniquely, that's, there is more than one school te Am that meets the properties described above. In this case, the region needs to undertake an additional contest. The "teams in the" considered to be different if there was at least one schoolboy who was included in one team And is not a included in the other team. It is guaranteed this for each of the region at least and 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 Formation requires additional contests.

Input
The first line of the input contains the integers n and M (2?≤?n?≤?100?000, 1?≤?m?≤?10?000, n?≥?2m)-the number of partic Ipants 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 str ing of length from 1 to characters and consisting of large and small 中文 letters), Region number (integer from 1 to m) and the number of points scored by the participant (integer from 0 to inclusive).

It is guaranteed this all surnames of the participants be distinct and at least, people participated from each of The M regions. The surnames that is differ in letter cases, should is considered distinct.

Output
Print m lines. On the i-th line print the team of the i-th Region-the surnames of the the and the team members in an arbitrary order, or a sing Le 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
The first sample region teams is uniquely determined.

In the second sample, the team from Region 2 are uniquely determined and the team from Region 1 can have three teams: "Petro V "-" Sidorov "," Ivanov "-" Sidorov "," Ivanov "-" Petrov ", so it's impossible to determine a team uniquely.

Main topic:
is to give n names, M places, and then we need to select two maximum points per place, the output is the name of the two largest fraction, if the second and third scores are the same, we will output '? '

Problem Solving Ideas:
This problem is actually a water problem, and then we are the order on the line, first put the M-area sequence, and then the score from the big to the small row sequence, and then is judged on the line. Note that there may be less than 2 people, then also output '? ', there is a small trick is to put Arr[n].id==-1, because each time we judge the current ID is not the same as its previous, so finally to the n that is assigned to 1, there are two ways to refer to:
The first one:

#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>using namespace STD;Const intMAXN =1e5+5;structsa{intID, SC;Charname[ the];} ARR[MAXN];BOOLCMP (sa A, sa b) {if(a.id = = b.id)returnA.sc > B.Sc;returna.ID < b.id;}intMain () {intN, M; while(Cin&GT;&GT;N&GT;&GT;M) { for(intI=0; i<n; i++) {Cin>>arr[i].name>>arr[i].id>>arr[i].sc; } sort (arr, arr+n, CMP);intD =0; Arr[n].id =-1;// Small tricks         for(intI=1; i<=n; i++) {if(Arr[i].id! = arr[i-1].id) {if(I-d <2)                {puts("?"); d = i;Continue; }if(I-d = =2)/// need attention here{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;}

The second one is written in a vector array:

#include <iostream>#include <algorithm>#include <vector>#include <cstdio>#include <cstdlib>using namespace STD;Const intMAXN =1e5+5;structsa{intID, SC;Charname[ the];} ARR[MAXN];BOOLCMP (sa A, sa b) {if(a.id = = b.id)returnA.sc > B.Sc;returna.ID < b.id;} vector <sa>VEC[MAXN];intMain () {intN, M; while(Cin&GT;&GT;N&GT;&GT;M) { for(intI=0; i<n; i++) {Cin>>arr[i].name>>arr[i].id>>arr[i].sc;        Vec[arr[i].id].push_back (Arr[i]); } for(intI=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;}

Codeforces 659 B. Qualifying Contest (Problem of structural sorting)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.