- PAT Ranking (25)
时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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
//sourcecode
//yhf_zju314_2016.11.16
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
class student{
public:
string ID;
int local_rank;
int score;
int location_number;
int final_rank;
bool operator > (const student& s)
{
if(score != s.score)
return score > s.score;
else return ID < s.ID;
}
friend bool compare(student a,student b);
};
bool compare(student a,student b)
{
if(a.score != b.score)
return a.score > b.score;
else return a.ID < b.ID; //升序排列,如果改为return a>b,则为降序
}
int main(){
int N,i,j;
cin>>N;
if(N==0)
return 0;
student *stu=new student[30000];int k=0;
student temp;
int time;
int *rank=new int[N+1];
int *t=new int[N+1];
int *t1=new int[N+1];
for(i=0;i<N+1;i++){
rank[i]=1;
t[i]=-1;
t1[i]=1;
}
for(i=0;i<N;i++){
cin>>time;
for(j=0;j<time;j++){
cin>>stu[k].ID;
stu[k].location_number=i+1;
cin>>stu[k].score;
k++;
}
}
sort(stu,stu+k,compare);
/* if(k<20){
for(i=0;i<k;i++){
for(j=i;j<k;j++){
if(stu[i].score<stu[j].score){
temp=stu[i];
stu[i]=stu[j];
stu[j]=temp;
}
if(stu[i].score==stu[j].score&&stu[i].ID>stu[j].ID){
temp=stu[i];
stu[i]=stu[j];
stu[j]=temp;
}
}
}
}
else{
int c;
c=k/2;
c=2*((c+1)/2)+3;
while(c!=-1){
for(i=0;i<k-c;i++){
j=i+c;
if(stu[i].score<stu[j].score){
temp=stu[i];
stu[i]=stu[j];
stu[j]=temp;
}
if(stu[i].score==stu[j].score&&stu[i].ID>stu[j].ID){
temp=stu[i];
stu[i]=stu[j];
stu[j]=temp;
}
}
c-=2;
}
}*/
stu[0].final_rank=1;
stu[0].local_rank=1;
t[stu[0].location_number]=stu[0].score;
t1[stu[0].location_number]=stu[0].location_number;
rank[stu[0].location_number]++;
cout<<k<<endl;
for(i=1;i<k;i++){
if(stu[i].score==stu[i-1].score)
stu[i].final_rank=stu[i-1].final_rank;
else
stu[i].final_rank=i+1;
if(stu[i].score<t[stu[i].location_number])
stu[i].local_rank=rank[stu[i].location_number];
else
stu[i].local_rank=t1[stu[i].location_number];
rank[stu[i].location_number]++;
t[stu[i].location_number]=stu[i].score;
t1[stu[i].location_number]=stu[i].local_rank;
}
for(i=0;i<k;i++)
cout<<stu[i].ID<<" "<<stu[i].final_rank<<" "<<stu[i].location_number<<" "<<stu[i].local_rank<<endl;
return 0;
}
感想:这题最重要的是最后的一个时间限制测试点。注掉的部分是我原来写的,原本直接O(N2)的排序,最后一个3万的测试点超时,然后临时去学了希尔排序,用进去,注掉的部分就是希尔排序有兴趣可以看看,虽然快了一倍但是并没有什么卵用,最后参照大神用了C++的库函数sort,效率简直提升的不是一点点,既然有sort函数我踏马还累死累活又是希尔排序又是快速排序的干吊TAT=======
本文sort函数用法
对于数组*a从0到N
sort(a,a+N,compare)可以把a以compare为排序依据进行排序,compare可以用友元函数重载。(注意这里是N而不是N-1)最后终于以116ms通过