分析:快乐模拟。先按身高降序排序,再确定好每行元素之后,又给出了每一排的起始位置,然后向左向右安排,循环直到达到上界和下届为止。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 1e4+10;
int group[12][10010];
int len[14], cnt;
struct Stu{
string name;
int height;
}stu[maxn];
int cmp(Stu a, Stu b){
return a.height != b.height ? a.height > b.height : a.name < b.name;
}
void formGroup(int row, int tot){
int mid = int(tot*1.0/2+1.4);
group[row][mid] = cnt++;
int l = mid-1, r = mid+1;
while(l>=1 || r<=tot){
if(l >= 1) group[row][l--] = cnt++;
if(r <= tot) group[row][r++] = cnt++;
}
}
int main(){
//freopen("aa.txt", "r", stdin);
ios::sync_with_stdio(false);
int n, k;
cin >> n >> k;
for(int i = 0; i<n; i++){
cin >> stu[i].name >> stu[i].height;
}
sort(stu, stu+n, cmp);
int m = int(n*1.0/k+0.5);
for(int i = 0; i<k; i++){
len[i] = i == 0 ? n-(k-1)*m : m;
formGroup(i, len[i]);
}
for(int i = 0; i<k; i++){
for(int j = 1; j<=len[i]; j++){
cout << stu[group[i][j]].name;
j != len[i] ? cout << " " : cout << '\n';
}
}
return 0;
}