您的位置:首页 > 其它

1025. PAT Ranking (25)

2016-07-06 15:07 344 查看
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


给出n个考场,每个考场有若干个学生,求出总的学生数,以及按照全部学生的成绩进行排序,输出学生的学号,总排名,考场号和地方排名。

1.用结构体表示学生的信息,对于每个考场,输入每个学生的学号,考场号和成绩;

2.用sort按照成绩排序;

3.输出结果,注意分数一样的排名一样,这里求地方排名要折腾一下,用三个数组维护地方的当前最后排名,地方当前排名最后的分数和地方当前访问过的人数,通过这三个数组得到每个人的地方排名,然后输出结果。

代码:

#include <iostream>
#include <vector>
#include <cstring>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
using namespace std;

struct testee
{
string id;
int location;
int score;
};

bool cmp(const testee &t1,const testee &t2)
{
if(t1.score!=t2.score) return t1.score>t2.score;
else return t1.id<t2.id;
}

int main()
{
int n;
cin>>n;
vector<testee>testees;
for(int i=1;i<=n;i++)
{
int k;
cin>>k;
for(int j=1;j<=k;j++)
{
testee t;
t.location=i;
cin>>t.id>>t.score;
testees.push_back(t);
}
}
sort(testees.begin(),testees.end(),cmp);
int m=testees.size();
cout<<m<<endl;
vector<int>local_rank(n+1,1);
vector<int>score(n+1,1000);
vector<int>count(n+1,1);
int rank=1;
//cout<<testees[0].id<<" 1 "<<testees[0].location<<" 1"<<endl;
for(int i=0;i<m;i++)
{
int loc=testees[i].location,lrank;
if(i==0) rank=1;
else if(testees[i].score<testees[i-1].score) rank=i+1;
if(testees[i].score<score[loc])
{
lrank=local_rank[loc]=count[loc];
score[loc]=testees[i].score;
}
else
{
lrank=local_rank[loc];
}
count[loc]++;
cout<<testees[i].id<<" "<<rank<<" "<<testees[i].location<<" "<<lrank<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sort