注意两点:
1.每行中间的最高,之后每行插入的人按照身高排序插入,由于是面对该Group,放在tallest person右边的人应插入到该person前面,即数组左边。
2.身高相同的情况,compare其名字,较小的优先插入。
利用sort排序,cmp比较需要仔细思考一下。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
struct node{
string name;
int height;
};
vector<node> v;
bool cmp(node a, node b){
if(a.height!=b.height)
return a.height<b.height;
else
return a.name>b.name;
}// define the compare function
int main(){
int N,K;
cin>>N>>K;
v.resize(N);
for(int i=0;i<N;i++)
{
cin>>v[i].name;
cin>>v[i].height;
}
sort(v.begin(),v.end(),cmp);
int s=N/K;//to get the number of people of each row;
int last=N-s*(K-1);// get the rear row's number
string rear[last];//to save the name order of the rear row
int count=0;
int m1=1;
int m2=1;
for(int j=0;j<last;j++)
{
if(j==0)
rear[last/2]=v[N-1].name;
else{
if(count%2==0){
rear[last/2-m1]=v[N-1-j].name;
m1++;
}
else
{
rear[last/2+m2]=v[N-1-j].name;
m2++;
}
count++;
}
}
for(int i=0;i<last;i++){
cout<<rear[i];
if(i!=last-1)
cout<<" ";
else
cout<<endl;
}
int now=N-1-last;
for(int i=0;i<K-1;i++){
string temp[s];
int count1=0;
int m3=1;
int m4=1;
for(int j=0;j<s;j++){
if(j==0)
temp[s/2]=v[now].name;
else{
if(count1%2==0){
temp[s/2-m3]=v[now-j].name;
m3++;
}
else
{
temp[s/2+m4]=v[now-j].name;
m4++;
}
count1++;
}
}
for(int m=0;m<s;m++){
cout<<temp[m];
if(m!=s-1)
cout<<" ";
else
cout<<endl;
}
now-=s;
}
return 0;
}