清華大学 - ソート結果(+ソート問題MLEを解決)

ソート結果

ソート結果

  • スーパーメモリの問題で、ここで注意すべきです。
  • 溶液は、ポインタの方法、サイズNオープンダイナミックメモリの各々はなく、冒頭を分配することによってです。
#include<bits/stdc++.h>
using namespace std;
int n;
const int maxn=1e6+6;
struct node{
    string name;
    int score;
    int level;
};
bool cmp1(const node& a,const node& b){//升序
    if(a.score==b.score){
        return a.level<b.level;
    }
    return a.score<b.score;
}
bool cmp2(const node& a,const node& b){//降序
    if(a.score==b.score){
        return a.level<b.level;
    }
    return a.score>b.score;
}
//node stu[maxn];
int main(){
    int kind;
    while(cin>>n>>kind){
        string s;
        node *stu=new node[n];
        int score;
        for(int i=0;i<n;i++){
            cin>>s>>score;
            stu[i].name=s;
            stu[i].score=score;
            stu[i].level=i;
        }
        if(kind==0){
            sort(stu,stu+n,cmp2);
        }else{
            sort(stu,stu+n,cmp1);
        }
        for(int i=0;i<n;i++){
            cout<<stu[i].name<<" "<<stu[i].score<<endl;
        }
    }
    return 0;
}

おすすめ

転載: www.cnblogs.com/GarrettWale/p/11521432.html