题目链接
大意:对多个考场的多个学生排名不仅有总排名还要有分区排名;
代码中有注释;
#pragma warning(disable:4996);
#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<stack>
#include<math.h>
#include<vector>
using namespace std;
struct stu {
char id[15];
int score;
int localnumber;//考场号;
int localrank;//所在考场内的排名;
bool operator<(const stu& a) const
{
if (a.score !=score) return a.score <score;
else return strcmp(id, a.id) < 0;
}
}buf[30010];
//现按分数由大到小,若相同则利用准考证号由小到大决出排名;
int main() {
int n, k, num=0;
cin >> n;
for (int i = 1;i <= n;i++)
{
cin >> k;//考场人数;
for (int j = 0;j < k;j++) {
getchar();
cin >> buf[num].id >> buf[num].score;
buf[num].localnumber = i;
num++;
}
sort(buf + num - k, buf + num);//利用k和num同一考场内的进行排序
//这个考场考生所对应的下标为[num-k,num);
buf[num - k].localrank = 1;
//对考场其余人排名;
for (int j = num - k + 1;j < num;j++) {
if (buf[j].score == buf[j - 1].score)buf[j].localrank = buf[j - 1].localrank;
else {//如果与前一个学生不同分
buf[j].localrank = j - (num - k)+ 1;
}
}
}
printf("%d\n", num);//总人数
sort(buf, buf + num);//总排名;
int r = 1;
for (int i = 0;i < num;i++) {
if (i > 0 && buf[i].score != buf[i - 1].score) {
r = i + 1;//当与上一个人分数不同时,让r更新为人数加1;
}
printf("%s ", buf[i].id);
printf("%d %d %d\n", r, buf[i].localnumber, buf[i].localrank);
}
return 0;
}