solution
- 从后排开始输出,可以先把所有的学生进行排序(身高降序,名字升序),再按照每排的人数找到中间位置依次左右各一个进行排列
- 测试点3, 4, 5:k是小于10的正整数,则每排最多有1e4个人
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = 1e4 + 10;
struct stu{string name;int h;friend bool operator < (stu &s1, stu &s2){if(s1.h != s2.h) return s1.h > s2.h;return s1.name < s2.name;}
}stus[maxn];
int main(){int n, k, now, cnt = 1, mid, c, flag;string ans[maxn];cin >> n >> k;for(int i = 1; i <= n; i++){cin >> stus[i].name >> stus[i].h;}sort(stus + 1, stus + n + 1);for(int i = 1; i <= k; i++){//i排数 if(i == 1) now = n / k + n % k;//now每行的人数 else now = n / k;mid = now / 2 + 1;//mid中间位置 flag = c = 1;//flag本排是否需要继续,c距离中间的距离 ans[mid] = stus[cnt++].name; while(flag){if(mid - c > 0) ans[mid - c] = stus[cnt++].name;if(mid + c < now + 1) ans[mid + c] = stus[cnt++].name;else if(mid - c < 1) flag = 0;c++; } for(int j = 1; j <= now; j++){if(j != 1) cout << " ";cout << ans[j];}cout << endl;}return 0;
}