AcWing 1599. 合影
原题链接
中等
作者:
巨鹿噜噜噜路
,
2020-06-02 15:04:42
,
所有人可见
,
阅读 668
C++ 代码
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX_N = 10010;
const int MAX_K = 11;
int group[MAX_K][MAX_N];
struct People {
char name[10];
int high;
bool operator<(const People b) const {
if (high != b.high) return high > b.high;
return strcmp(name, b.name) < 0;
}
} p[MAX_N];
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%s%d", p[i].name, &p[i].high);
}
sort(p, p + n);
int idx = 0;
for (int i = 0; i < k; i++) {
int len = round(n / k);
//最后一排
if (i == 0) len += n % k;
vector<int> line(len + 1);
int mid = len / 2 + 1; //中间的人
for (int r = mid, l = mid - 1; l > 0 || r <= len; l--, r++) {
//从中间的开始向两边枚举
if (r <= len) line[r] = idx++;
if (l > 0) line[l] = idx++;
}
for (int j = 1; j <= len; j++) {
if (j != 1) printf(" ");
printf("%s", p[line[j]].name);
}
printf("\n");
}
return 0;
}