Formation is very important when taking a group photo. Given the rules of forming K rows with N people as the following:
The number of people in each row must be N/K (round down to the nearest integer), with all the extra people (if any) standing in the last row;
All the people in the rear row must be no shorter than anyone standing in the front rows;
In each row, the tallest one stands at the central position (which is defined to be the position (m/2+1), where m is the total number of people in that row, and the division result must be rounded down to the nearest integer);
In each row, other people must enter the row in non-increasing order of their heights, alternately taking their positions first to the right and then to the left of the tallest one (For example, given five people with their heights 190, 188, 186, 175, and 170, the final formation would be 175, 188, 190, 186, and 170. Here we assume that you are facing the group so your left-hand side is the right-hand side of the one at the central position.);
When there are many people having the same height, they must be ordered in alphabetical (increasing) order of their names, and it is guaranteed that there is no duplication of names.
Now given the information of a group of people, you are supposed to write a program to output their formation.
Input Specification:
Each input file contains one test case. For each test case, the first line contains two positive integers N (≤10^4), the total number of people, and K (≤10), the total number of rows. Then N lines follow, each gives the name of a person (no more than 8 English letters without space) and his/her height (an integer in [30, 300]).
Output Specification:
For each case, print the formation – that is, print the names of people in K lines. The names must be separated by exactly one space, but there must be no extra space at the end of each line. Note: since you are facing the group, people in the rear rows must be printed above the people in the front rows.
Sample Input:
10 3
Tom 188
Mike 170
Eva 168
Tim 160
Joe 190
Ann 168
Bob 175
Nick 186
Amy 160
John 159
Sample Output:
Bob Tom Joe Nick
Ann Mike Eva
Tim Amy John
题意
将K个人排成K排(剩下的人放在第一排)。每一排的人的身高必须低于或者等于必上一排的人,相同身高按字典序升序排列。一排中中间的位置被定义为[m / 2] + 1。然后按左右左右的顺序排列。
思路
首先先将所有人按规则排序,计算出每排的人数len,中间的位置为[len / 2](因为我们的下标从0开始),左右指针l,r初始指向中间,一个布尔变量用于控制左右的切换。我们可以将中间认为是右边的位置,这样排列变成右左右左。对于指针r,先放再加,对于指针l,先减再放(防止初始时和r叠在一起),当然这里指针的使用和设置不唯一。
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX_N 10000
using namespace std;
char names[MAX_N][9];
int height[MAX_N], pq[MAX_N], row[MAX_N];
int cmp(int a, int b){
return height[a] != height[b] ? height[a] > height[b] : strcmp(names[a], names[b]) < 0;
}
int main() {
int N, K;
scanf("%d %d", &N, &K);
for(int i = 0; i < N; i++){
pq[i] = i;
scanf("%s %d", names[i], &height[i]);
}
sort(pq, pq + N, cmp);
int m = N / K, left = N % m;
for(int i = 0, j = 0, len, high, l, r; i < K; i++){
len = i == 0 && left > 0 ? m + left : m; // 计算当前行的人数
high = j + len; // 计算下标上限
l = r = len / 2; // 初始化左右指针
// 按规则将人排序在row[0]~row[len]中
bool turn = true;
for(;j < high; j++){
if(turn){
row[r++] = pq[j];
}else{
row[--l] = pq[j];
}
turn = !turn;
}
// 输出结果
for(int k = 0; k < len; k++){
if(k > 0) putchar(' ');
printf("%s", names[row[k]]);
}
putchar('\n');
}
return 0;
}