acwing4711.排队
假设one和oke按某种排序方式,他们是相等的,如果使用的sort排序,在给他们排序后oke可能会出现在one的后面,而你使用stable_sort排序,你会发现他们的次序一定是不变的,保留原有的顺序。
#include <iostream>
#include <cstring>
#include <unordered_map>
#include <algorithm>
using namespace std;
int n;
struct node{
string name,identity;
int p;
}node[10001];
bool cmp(const struct node& n1,const struct node& n2){
return n1.p<n2.p;
}
int main()
{
cin>>n;
unordered_map<string,int> umap;
umap["rat"]=1;
umap["woman"]=umap["child"]=2;
umap["man"]=3;
umap["captain"]=4;
for(int i=0;i<n;i++){
string a,b;
cin>>a>>b;
node[i].name=a;
node[i].identity=b;
node[i].p=umap[b];
}
stable_sort(node,node+n,cmp);
for(int i=0;i<n;i++){
cout<<node[i].name<<endl;
}
}
这里给出答案,大家可以试试sort,有些测试会过不去的!