题目描述
简单的排序模拟,也没啥细节,想明白了写就好了。
每个区域处理一遍排序和排名,然后再全体处理一遍排序和排名就行了。
时间复杂度
复杂度瓶颈在排序上,是$O(nlogn)$。
C++ 代码
#include <iostream>
#include <algorithm>
using namespace std;
int n, k, c;
struct S
{
string registration_number;
int final_rank;
int location_number;
int local_rank;
int score;
bool operator<(S s)
{
if (score != s.score) return score >= s.score;
return registration_number < s.registration_number;
}
}s[30010];
void gen_local_rank(S* l, S* r)
{
int num = 2;
int rank = 1;
S* i = l + 1;
l -> local_rank = 1;
while (i != r)
{
if ((i - 1) -> score == i -> score)
i -> local_rank = rank;
else
i -> local_rank = rank = num;
num ++, i ++;
}
}
void gen_final_rank(S* l, S* r)
{
int num = 2;
int rank = 1;
S* i = l + 1;
l -> final_rank = 1;
while (i != r)
{
if ((i - 1) -> score == i -> score)
i -> final_rank = rank;
else
i -> final_rank = rank = num;
num ++, i ++;
}
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i ++)
{
cin >> k;
for (int j = 0; j < k; j ++)
{
string rn;
int sc;
cin >> rn >> sc;
s[c ++] = {rn, 0, i, 0, sc};
}
sort(s + c - k, s + c);
gen_local_rank(s + c - k, s + c);
}
sort(s, s + c);
gen_final_rank(s, s + c);
cout << c << endl;
for (int i = 0; i < c; i ++)
cout << s[i].registration_number << " " << s[i].final_rank << " " << s[i].location_number << " "<< s[i].local_rank << endl;
}