Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it is your job to write a program to correctly merge all the ranklists and generate the final rank.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.
Output Specification:
For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:
registration_number final_rank location_number local_rank
The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.
Sample Input:
2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85
Sample Output:
9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4
排序题可以用sort()函数解决,本题的关键在于如果分数一样,那么排名也要相同,
但接下来的人排名要顺延,比如a 98;b 98 ;c 97,刚开始写算法吧a,b,c排名输出为1 1 2,而不是1 1 3,以后别犯这么简单的失误了!!!
输出顺序是分数由大到小,分数一样看id,id的字典序小的则先输出,所以id用string来定义,这样可以直接比较字典序。
#include <iostream> #include<stdio.h> #include<string> #include<algorithm> using namespace std; struct student{ string id; int scroce; int rk;//记录总排名 int room; int room_rk;//记录房间排名 }; bool cmp(student a,student b){ if(a.scroce!=b.scroce) return a.scroce>b.scroce; else return a.id<b.id;//string直接字典序比较,不用char里的strcmp; } int main() { student stu[99999];//反正内存64M不如大胆点 int n,i,s,t,a,b,x; cin>>n; a=b=0; for(i=0;i<n;i++){ cin>>s; for(t=0;t<s;t++){ cin>>stu[a].id>>stu[a].scroce; stu[a].room=i+1; a++;//a为总人数。 } sort(stu+b,stu+a,cmp);//(sub+b,stu+a)为一个房间内的考生。 stu[b].room_rk=1; for(x=b+1;x<a;x++){ if(stu[x].scroce==stu[x-1].scroce) stu[x].room_rk=stu[x-1].room_rk;//分数相同,则排名一致。 else stu[x].room_rk=x+1-b; } b=a; } sort(stu,stu+a,cmp);//总的考生再排序一遍。 int r=1; stu[0].rk=1; for(i=1;i<a;i++){ if(stu[i].scroce==stu[i-1].scroce) stu[i].rk=stu[i-1].rk; else stu[i].rk=i+1; } cout<<a<<endl; for(i=0;i<a;i++){ cout<<stu[i].id<<" "<<stu[i].rk<<" "<<stu[i].room<<" "<<stu[i].room_rk<<endl; } }