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 / (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 (, 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 (≤), the total number of people, and K (≤), 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排,前K-1排每排站N/K个人,剩下的人全部站在后面的一排。每一排中要求中间位置(M / 2)站个子最高的那个,然后再将剩余的人中个子最高的依次放在这个人的右边和左边,因为拍照的时候我们时正对着他们的,所以在我们看来依次是左边和右边。后排的人一定要比前排的人要高。最后将这些人的名字按照要求输出即可。

思路:

  将所有的人先按照个子的高低顺序从高到低排序,如果个子相同则按照名字的字典序进行降序排序。然后进行模拟就好了,当然模拟也不是想到什么就实现什么,也要讲求一定的方法和策略。不然的话代码很容易出错。

Code:

 1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 struct Student {
6 string name;
7 int height;
8 } stu[10005];
9
10 bool cmp(Student a, Student b) {
11 if (a.height == b.height)
12 return a.name < b.name;
13 else
14 return a.height > b.height;
15 }
16
17 int main() {
18 int n, k;
19 cin >> n >> k;
20 for (int i = 0; i < n; ++i) {
21 cin >> stu[i].name >> stu[i].height;
22 }
23 sort(stu, stu+n, cmp);
24 int count = k, m, t = 0;
25 while (count) {
26 if (count == k) {
27 m = n - (n / k) * (k - 1);
28 } else {
29 m = n / k;
30 }
31 vector<Student> ans(m);
32 ans[m/2] = stu[t];
33 int j = m / 2 - 1;
34 for (int i = t + 1; i < t + m; i += 2)
35 ans[j--] = stu[i];
36 j = m / 2 + 1;
37 for (int i = t + 2; i < t + m; i += 2)
38 ans[j++] = stu[i];
39 cout << ans[0].name;
40 for (int i = 1; i < m; ++i)
41 cout << " " << ans[i].name;
42 cout << endl;
43 t += m;
44 count--;
45 }
46 return 0;
47 }

参考:

  https://blog.csdn.net/liuchuo/article/details/51985808

最新文章

  1. 2016-2017 ACM-ICPC Southwestern European Regional Programming Contest (SWERC 2016)
  2. OJ提交题目中的语言选项里G++与C++的区别(转)
  3. java提高篇(三)-----java的四舍五入
  4. ansible解密
  5. 如何去掉list里重复的数据
  6. KNN算法与Kd树
  7. HTTP状态码整理
  8. windows phone 7 中怎样定义和使用资源(Resource)
  9. android 学习随笔二十六(动画:属性动画)
  10. Spring框架学习之第8节
  11. Maven--(一个坑)在settings.xml文件中添加mirrors导致无法新建Maven项目
  12. js中iframe的用法
  13. SQL每个用户最后的一条记录
  14. 关于C语言中的强符号、弱符号、强引用和弱引用的一些陋见,欢迎指正
  15. WebRTC–getUserMedia-filter
  16. Python全栈之路-Day33
  17. 安装并配置Apache
  18. 获取日期Date
  19. ngxs 状态管理器
  20. C和C++中的volatile、内存屏障和CPU缓存一致性协议MESI

热门文章

  1. Vue框架简介及简单使用
  2. Python 过滤字母和数字
  3. SpringBoot(八):SpringBoot中配置字符编码 Springboot中文乱码处理
  4. 后端程序员之路 43、Redis list
  5. HDOJ-1074(动态规划+状态压缩)
  6. Java 基础加强 01
  7. weblogic弱口令+后台getshell
  8. Git代码分支开发工作流程
  9. Apache配置 10. 访问控制-禁止解析PHP
  10. centos安装rar