注意结构体的cmp函数的写法
先将所有人在v1中排好序(根据身高升序,同身高根据字母ASCLL码降序),实现v1中要先站位的人在后面
根据站行规则在v2[]中对应行插入结点,每插入一个,v1 pop_back()一个
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
string name;
int height;
};
bool cmp(const node &a,const node &b){
if(a.height==b.height){
return a.name>b.name;
}else{
return a.height<b.height;
}
}
vector<node> v1,v2[11];
int main(){
int N,K;
cin>>N>>K;
int num=N/K;
int num_last=num+(N%K);
for(int i=0;i<N;i++){
node x;
cin>>x.name>>x.height;
v1.push_back(x);
}
sort(v1.begin(),v1.end(),cmp);
for(int i=K;i>0;i--){
int Insert=1;
if(i==K){
for(int j=0;j<num_last;j++){
node x=v1[v1.size()-1];
if(Insert==0){
v2[i].insert(v2[i].begin(),x);
v1.pop_back();
Insert=1;
}else{
v2[i].push_back(x);
v1.pop_back();
Insert=0;
}
}
}else{
for(int j=0;j<num;j++){
node x=v1[v1.size()-1];
if(Insert==0){
v2[i].insert(v2[i].begin(),x);
v1.pop_back();
Insert=1;
}else{
v2[i].push_back(x);
v1.pop_back();
Insert=0;
}
}
}
}
for(int i=K;i>0;i--){
for(auto it=v2[i].begin();it!=v2[i].end();it++){
if(it==v2[i].begin()){
cout<<it->name;
}else{
cout<<" "<<it->name;
}
}
printf("\n");
}
return 0;
}