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 (<=10000), 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
题目大意:拍集体照时队形很重要,这里对给定的N个人K排的队形设计排队规则如下:
每排人数为N/K(向下取整),多出来的人全部站在最后一排;后排所有人的个子都不比前排任何人矮;每排中最高者站中间(中间位置为m/2+1,其中m为该排人数,除法向下取整);每排其他人以中间人为轴,按身高非增序,先右后左交替入队站在中间人的两侧(例如5人身高为190、188、186、175、170,则队形为175、188、190、186、170。这里假设你面对拍照者,所以你的左边是中间人的右边);若多人身高相同,则按名字的字典序升序排列。这里保证无重名。现给定一组拍照人,请编写程序输出他们的队形。输出拍照的队形。即K排人名,其间以空格分隔,行末不得有多余空格。注意:假设你面对拍照者,后排的人输出在上方,前排输出在下方~
分析:建立结构体node,里面包含string类型的姓名name和int类型的身高height~将学生的信息输入到node类型的vector数组stu中~然后对stu数组进行排序(cmp函数表示排序规则,如果身高不等,就按照身高从大到小排列;如果身高相等,就按照名字从小到大的字典序排列~)然后用while循环排列每一行,将每一行应该排列的结果的姓名保存在ans数组中~
因为是面对拍照者,后排的人输出在上方,前排输出在下方,每排人数为N/K(向下取整),多出来的人全部站在最后一排,所以第一排输出的应该是包含多出来的人,所以while循环体中,当row == k时,表示当前是在排列第一行,那么这一行的人数m应该等于总人数n减去后面的k列*(k-1)行,即m = n – n / k * (k-1);如果不是第一行,那么m直接等于n / k;最中间一个学生应该排在m/2的下标位置,即ans[m / 2] = stu[t].name;然后排左边一列,ans数组的下标 j 从m/2-1开始,一直往左j–,而对于stu的下标 i,是从t+1开始,每次隔一个人选取(即i = i+2,因为另一些人的名字是给右边的),每次把stu[i]的name赋值给ans[j–];排右边的队伍同理,ans数组的下标 j 从m/2 + 1开始,一直往右j++,stu的下标 i,从t+2开始,每次隔一个人选取(i = i+2),每次把stu[i]的name赋值给ans[j++],然后输出当前已经排好的ans数组~每一次排完一列row-1,直到row等于0时退出循环表示已经排列并输出所有的行~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
#include <iostream> #include <algorithm> #include <vector> using namespace std; struct node { string name; int height; }; int cmp(struct node a, struct node b) { return a.height != b.height ? a.height > b.height : a.name < b.name; } int main() { int n, k, m; cin >> n >> k; vector<node> stu(n); for(int i = 0; i < n; i++) { cin >> stu[i].name; cin >> stu[i].height; } sort(stu.begin(), stu.end(), cmp); int t = 0, row = k; while(row) { if(row == k) { m = n - n / k * (k - 1); } else { m = n / k; } vector<string> ans(m); ans[m / 2] = stu[t].name; // 左边一列 int j = m / 2 - 1; for(int i = t + 1; i < t + m; i = i + 2) ans[j--] = stu[i].name; // 右边一列 j = m / 2 + 1; for(int i = t + 2; i < t + m; i = i + 2) ans[j++] = stu[i].name; // 输出当前排 cout << ans[0]; for(int i = 1; i < m; i++) cout << " " << ans[i]; cout << endl; t = t + m; row--; } return 0; } |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼