题目描述
很容易的基于不同关键字的排序题。
输入输出量很大,有点卡常。
直接偷懒开O2了qwq
(排序) $O(nlogn)$
C++ 代码
#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
using namespace std;
int n, c;
struct S
{
string ID, name;
int score;
}s[1000010];
bool cmp1(S& a, S& b)
{
return a.ID < b.ID;
}
bool cmp2(S& a, S& b)
{
if (a.name != b.name) return a.name <= b.name;
return a.ID < b.ID;
}
bool cmp3(S& a, S& b)
{
if (a.score != b.score) return a.score <= b.score;
return a.ID < b.ID;
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> c;
for (int i = 0; i < n; i ++) cin >> s[i].ID >> s[i].name >> s[i].score;
if (c == 1) sort(s, s + n, cmp1);
else if (c == 2) sort(s, s + n, cmp2);
else if (c == 3) sort(s, s + n, cmp3);
for (int i = 0; i < n; i ++)
cout << s[i].ID << " " << s[i].name << " " << s[i].score << endl;
}
结构数组稍微开大了一点hh
还是出现了段错误,你还是稍微修改一下吧。