#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct stu{
string name;
int height;
};
bool cmp(stu a,stu b){
if(a.height!=b.height){
return a.height>b.height;
}else{
return a.name<b.name;
}
}
int main() {
int n,k,m;
cin>>n>>k;
vector<stu> a(n);
for(int i=0;i<n;i++){
cin>>a[i].name>>a[i].height;
}
sort(a.begin(),a.end(),cmp);
int t = 0, row = k;
while(row) {
if(row == k)
m = n - n / k * (k - 1);
else
m = n / k;
vector<string> ans(m);
ans[m / 2] = a[t].name; //m/2这一排中间的位置给这排最高的人
int j = m / 2 - 1; //中间位置开始往左排j--,在a数组中对应的学生位置+2,+1的学生排在右边
for(int i = t + 1; i < t + m; i = i + 2) //一排对应a中a[t]~a[t+m]
ans[j--] = a[i].name;
j = m / 2 + 1; //中间位置最近的右边位置,j++对应a数组+2的学生
for(int i = t + 2; i < t + m; i = i + 2)
ans[j++] = a[i].name;
cout << ans[0]; // 输出当前排
for(int i = 1; i < m; i++)
cout << " " << ans[i];
cout << endl;
t = t + m; //更新每排a对应的起始位置
row--;
}
system("pause");
return 0;
}
PAT 1055 集体照 (25 分)
最新推荐文章于 2024-06-09 20:18:37 发布