简单排序,先根据成绩排序,成绩相同,再根据编号排序即可。

代码:

#include<iostream>
#include<algorithm>
using namespace std;
const int MAX=1e5+10;
struct stu{
    int idx,score;
    char* c[200];
}s[MAX];
int cnt;
//两个比较函数
bool cmp0(stu& s1,stu& s2){
    return s1.score!=s2.score?s1.score>s2.score:s1.idx<s2.idx;
}
bool cmp1(stu& s1,stu& s2){
    return s1.score!=s2.score?s1.score<s2.score:s1.idx<s2.idx;
}
int main(){
    int n;
    while(~scanf("%d",&n)){
        int op;
        scanf("%d",&op);
        cnt=0;
        for(int i=1;i<=n;++i){
            scanf("%s",s[++cnt].c);
            scanf("%d",&s[cnt].score);
            s[cnt].idx=i;
        }
        //排序
        if(op)    sort(s+1,s+1+cnt,cmp1);
        else sort(s+1,s+1+cnt,cmp0);
        for(int i=1;i<=cnt;++i){
            printf("%s ",s[i].c);
            printf("%d\n",s[i].score);
        }
    }
}