【疑问】为什么这题必须用stable_sort?

P1104 生日

每比较编号
by c201904 @ 2017-08-10 17:29:07


stable\_sort是稳定排序,sort是不稳定排序 @[汪添翼](/space/show?uid=24617)
by 北海_Beihai @ 2017-08-14 21:19:33


@[大神犇](/space/show?uid=25279) 谢谢大佬
by 蒟蒻wyx @ 2017-08-26 21:44:52


@[汪添翼](/space/show?uid=24617) me too
by 蒟蒻wyx @ 2017-08-26 21:45:15


用结构体,sort先按天数排一遍,再按月数拍一遍,最后按年份排一遍
by MscWood @ 2017-09-10 15:32:36


@[一棵线段树](/space/show?uid=25279) 那我为什么冒泡也wa了
by Tarensev @ 2018-01-14 19:09:37


@[馒头精](/space/show?uid=57925) 冒泡是 O(n^2)
by 北海_Beihai @ 2018-01-24 19:53:20


@[馒头精](/space/show?uid=57925) 我刚才说错了。 如果用冒泡排序,要多记录输入顺序。可以写一个结构体并自定义一个小于号: ```cpp struct node{ string name; int y,m,d,id; //依次是:年,月,日,输入顺序 bool operator <(const node& x) const{ //小于号 if(y<x.y) return 1; if(y>x.y) return 0; if(m<x.m) return 1; if(m>x.m) return 0; if(d<x.d) return 1; if(d>x.d) return 0; if(id>x.id) return 1; return 0; } } ```
by 北海_Beihai @ 2018-01-24 20:00:37


我用sort也过了呀 ```cpp #include<bits/stdc++.h> using namespace std; struct birthday { string name; int y,m,d,id; }a[105]; bool cmp(birthday a,birthday b) { if(a.y==b.y) { if(a.m==b.m) { if(a.d==b.d)return a.id>b.id; else return a.d<b.d; } else return a.m<b.m; } else return a.y<b.y; } int main() { int n; cin>>n; for(int i=1;i<=n;i++) { cin>>a[i].name>>a[i].y>>a[i].m>>a[i].d; a[i].id=i; } sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++) cout<<a[i].name<<endl; return 0; } ```
by weak_ddb @ 2018-04-09 20:27:42


|