Using qsort() for stable sort?

后端 未结 2 934
闹比i
闹比i 2021-01-24 08:30

I was trying to solve a problem in a online judge system: https://acm.cs.nthu.edu.tw/problem/11519/ It takes an integer n, followed with n lines of name and grade. the problem i

2条回答
  •  死守一世寂寞
    2021-01-24 09:03

    It ends up that I missed a line in the description:

    The input includes multiple test cases. In each test case, the first line contains one integer N.

    After several approaches, I got it passed with the following code:

    class People{
        public:
            char name[11];
            int grade;
            int order;
            void print(){
                printf("%s %d\n", name, grade);
            }
    };
    
    int compar(const void *a, const void *b){
        People *A = (People*)a;
        People *B = (People*)b;
        if(A->grade > B->grade) return 1;
        else if(A->grade < B->grade) return -1;
        else if(A->order > B->order) return 1;
        else if(A->order < B->order) return -1;
        return 0;
    }
    
    int main(){
        int n;
        while(scanf("%d", &n)!=EOF){
            People *p = new People[n];
            for(int i=0;i

    std::stable_sort just works, However, it received TLE(Time Limit Exceeded),and the cin/cout causes TLE, too. So that's the reason I stick with printf/scanf.

    Thanks everyone for answering this question! :)

提交回复
热议问题