AcWing 3375. 成绩排序
原题链接
简单
作者:
故事里的大魔王
,
2025-01-10 12:37:19
,
所有人可见
,
阅读 1
#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
using namespace std;
typedef struct student{
string name;
int score;
int origin;
} stu;
bool upper(stu l, stu r){
if(l.score < r.score) return true;
else if(l.score == r.score && l.origin < r.origin) return true;
else return false;
}
bool down(stu l, stu r){
if(l.score > r.score) return true;
else if(l.score == r.score && l.origin < r.origin) return true;
else return false;
}
int main(){
int n, flag;
cin >> n >> flag;
vector<stu> school;
for(int i = 0; i < n; ++ i){
stu tmp;
tmp.origin = i;
cin >> tmp.name >> tmp.score;
school.push_back(tmp);
}
if(flag == 1) sort(school.begin(), school.end(), upper);
else sort(school.begin(), school.end(), down);
for(int i = 0; i < n; ++ i)
cout << school[i].name << " " << school[i].score << endl;
return 0;
}