|
|
back to boardWhy my code doesn't work with c++ 'sort' ? But it works fine with stable_sort o_O /* * @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 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 OK, i've found yhe answere - stable_sort keeps the relative order berween equal elements... because this is stable sort)) |
|
|