ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1100. Final Standings

Why my code doesn't work with c++ 'sort' ? But it works fine with stable_sort o_O
Posted by sirius_lyra 9 Feb 2018 13:50
/*
* @Author: eleven
* @Date:   2017-05-21 02:50:38
* @Last Modified by:   eleven
* @Last Modified time: 2018-02-09 14:49:14
*/

// Status : AC ( works with stable_sort )
// doesn't works with sort

#include <bits/stdc++.h>

using namespace std;

#define SIZE 150005

struct team{
    int id;
    int solved;
}teams[SIZE];

bool foo(team lhs, team rhs){
return lhs.solved > rhs.solved;
}

void print(int n ){

    for(int i= 0; i<n ; ++i){
        cout<<teams[i].id<<" "<<teams[i].solved<<'\n';
    }
}


int main(){


    //freopen("in","r",stdin);
    //freopen("out","w",stdout);

    int n ;

    cin>>n;

    for(int i = 0; i<n ; ++i){
        cin>>teams[i].id>>teams[i].solved;
    }

    stable_sort(teams,teams+n ,foo);

    print(n);

    return 0;

}
Re: Why my code doesn't work with c++ 'sort' ? But it works fine with stable_sort o_O
Posted by bstu_student 9 Aug 2018 06:05
I don't know why, but i have tha same problem. WA with sort, AC with stable_sort *go to read faq's*

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
using namespace std;

pair <int, int> a[150000];
int n;

bool comp(pair <int, int> a, pair <int, int> b)
{
    return a.first > b.first;
}
int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%d%d", &a[i].second, &a[i].first);
    stable_sort(a, a + n, comp);
    //sort(a, a+n,comp);
    for (int i=0;i<n;i++)
        printf("%d %d\n", a[i].second, a[i].first);
    return 0;
}
Re: Why my code doesn't work with c++ 'sort' ? But it works fine with stable_sort o_O
Posted by bstu_student 9 Aug 2018 06:09
OK, i've found yhe answere - stable_sort keeps the relative order berween equal elements... because this is stable sort))