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
分析:
1、经典排序,结构体多条件排序,并且用到了vector分段排序。
2、sort里迭代器v.begin()可以加减数字,而在for遍历时迭代器不能auto it = v.begin() + 1;
我的代码:
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
struct Person
{
char id[15];
int number,grade,rank[2];//rank[0]里存储总名次,rank[1]存储各部分名次;number为地区号
}person;
vector<Person> v;
bool cmp(const Person &p1,const Person &p2)
{
if (p1.grade != p2.grade)
return p1.grade > p2.grade;
else
return strcmp(p1.id,p2.id) < 0;
}
void setRank(int temp,int r)
{
int i = 2;
v[temp].rank[r] = 1;
for (int j = temp + 1 ;j < v.size() ;j++)
{
if (v[j].grade == v[j - 1].grade)
v[j].rank[r] = v[j - 1].rank[r];
else
v[j].rank[r] = i;
i++;
}
}
int main()
{
int n,temp = 0;
scanf("%d",&n);
for (int i = 1 ;i <= n ;i++)
{
int k;
scanf("%d",&k);
for (int j = 0 ;j < k ;j++)
{
scanf("%s %d",person.id,&person.grade);
person.number = i;
v.push_back(person);
}
sort(v.begin() + temp,v.end(),cmp);
setRank(temp,1);
temp += k;
}
sort(v.begin(),v.end(),cmp);
setRank(0,0);
printf("%d\n",v.size());
for (int i = 0 ;i < v.size() ;i++)
printf("%s %d %d %d\n",v[i].id,v[i].rank[0],v[i].number,v[i].rank[1]);
return 0;
}