您的位置:首页 > 其它

solution Of 1025. PAT Ranking (25)

2016-05-17 01:39 274 查看
1025. PAT Ranking (25)

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

结题思路 :

题意要求我们找到对pat考生的成绩进行排序。

要求1:由于结构体部分保存的信息较多,不对结构体数组直接进行排序,而是对指针数组进行排序;

要求2:第二趟关于全局部分的排序采用归并的方式来加快排序的进行;

要点3:最后部分需要输出参考人员数量。

程序步骤:

第一步、将每个地区的数据暂存,得到地区排名;

第二步、通过归并的形式将地区数据整合,得到全局排名。

具体程序(AC)如下:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>

using namespace std;
struct node{
char no[15];//人员编号
int location;//地区编号
int rankLocal;//当地排名
int score;
};
vector<vector<node> >stu;//考生数组
vector<vector<node*> >stuP;//考生指针数组
int cmp(node* a,node* b)//成绩从大到小,学号从小到大
{
if(a->score==b->score)
return strcmp(a->no,b->no)<0;
return a->score>b->score;
}
void rankPart(vector<node*>& local)
{
int length=local.size();
int start=0;
int value=-1;
for(int i=0;i<length;++i)
{
if(local[i]->score==value)
local[i]->rankLocal=start;
else
{
local[i]->rankLocal=i+1;
start=i+1;
value=local[i]->score;
}
}
}
void rankAll(vector<node*>& local)
{
int length=local.size();
int start=0;
int value=-1;
for(int i=0;i<length;++i)
{
if(local[i]->score!=value)
{
start=i+1;
value=local[i]->score;
}
cout<<local[i]->no<<" "<<start<<" "<<local[i]->location<<" "<<local[i]->rankLocal<<endl;
}
}
int main()
{
int N,K;
int i,j,len;
vector<node> tmp;
vector<node*> tmpP;
while(cin>>N)
{
len=0;
stu.clear();
stuP.clear();
for(i=0;i<N;++i)
{
cin>>K;
len+=K;
tmp.clear();
tmpP.clear();
tmp.resize(K);
tmpP.resize(K);
for(j=0;j<K;++j)
{
cin>>tmp[j].no>>tmp[j].score;
tmp[j].location=i+1;
}
stu.push_back(tmp);
for(j=0;j<K;++j)
tmpP[j]=&stu[i][j];//对指针数组赋值
sort(tmpP.begin(),tmpP.end(),cmp);//地区排序
rankPart(tmpP);
stuP.push_back(tmpP);
}
vector<node*> result;
vector<node*> finalResult;
for(i=0;i<N;++i)//全局排序
{
result.clear();
result.resize(finalResult.size()+stuP[i].size());
merge(finalResult.begin(),finalResult.end(),stuP[i].begin(),stuP[i].end(),result.begin(),cmp);
finalResult=result;
}
cout<<len<<endl;
rankAll(finalResult);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: