AcWing 1502. PAT 排名
原题链接
中等
作者:
leo123456
,
2020-08-20 17:14:19
,
所有人可见
,
阅读 458
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int N=110;
struct Student
{
string id;
int grade;
int location_number, local_rank, final_rank;
};
bool cmp(Student a, Student b){
if(a.grade != b.grade) return a.grade > b.grade;
else return a.id < b.id;
}
vector<Student> grades[N];
vector<Student> all;
int main()
{
int n;
cin >> n;
for (int i = 1; i <= n; i ++ )
{
int k;
cin >> k;
for (int j = 0; j < k; j ++ )
{
string id;
int grade;
cin >> id >> grade;
grades[i].push_back({id, grade, i});
}
auto& g = grades[i];
sort(g.begin(), g.end(),cmp);
for (int i = 0; i < g.size(); i ++ )
{
if (!i || g[i].grade != g[i - 1].grade)
g[i].local_rank = i + 1;
else
g[i].local_rank = g[i - 1].local_rank;
all.push_back(g[i]);
}
}
sort(all.begin(), all.end(),cmp);
for (int i = 0; i < all.size(); i ++ )
if (!i || all[i].grade != all[i - 1].grade) //排名计算方法 1:if一个排名||后面成绩不等于前面
all[i].final_rank = i + 1; //3:排名为i+1
else
all[i].final_rank = all[i - 1].final_rank; //2:后面如果等于前面排名也等于前
cout << all.size() << endl;
for (auto& s : all)
cout << s.id << ' ' << s.final_rank << ' ' << s.location_number << ' ' << s.local_rank << endl;
return 0;
}