这次做的比去年还多六行代码,就放去年的代码吧。
#include<bits/stdc++.h>
using namespace std;
struct student {
string name;
int h;
}Student;
int cntS = -1;
vector<student> v;
vector<vector<student>> ans;
void GP (int nowRow, int n) {
int pos = n / 2;
ans[nowRow].resize(n);
for (int i = pos, j = pos - 1; i < n || j >= 0; i++, j--) {
if (i < n) ans[nowRow][i] = v[++cntS];
if (j >= 0) ans[nowRow][j] = v[++cntS];
}
}
bool cmp (student a, student b) {
if (a.h != b.h) return a.h > b.h;
else return a.name < b.name;
}
int main() {
int n, k, rowNum, rm;
cin >> n >> k;
rowNum = n / k;
rm = n % k;
ans.resize(k);
v.resize(n);
for (int i = 0; i < n; i++) {
cin >> Student.name >> Student.h;
v[i] = Student;
}
sort (v.begin(), v.end(), cmp);
for (int i = 0; i < k; i++) {
if (i == 0) GP (i, rowNum + rm);
else GP (i, rowNum);
}
for (int i = 0; i < k; i++) {
for (int j = 0; j < ans[i].size(); j++) {
cout << ans[i][j].name;
if (j != ans[i].size() - 1) cout << ' ';
}
cout << endl;
}
}