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

1109 Group Photo (25分)的更多相关文章

  1. 【PAT甲级】1109 Group Photo (25分)(模拟)

    题意: 输入两个整数N和K(N<=1e4,K<=10),分别表示人数和行数,接着输入N行每行包括学生的姓名(八位无空格字母且唯一)和身高([30,300]的整数).按照身高逆序,姓名字典序 ...

  2. 1109. Group Photo (25)

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  3. PAT (Advanced Level) 1109. Group Photo (25)

    简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  4. PAT甲题题解-1109. Group Photo (25)-(模拟拍照排队)

    题意:n个人,要拍成k行排队,每行 n/k人,多余的都在最后一排. 从第一排到最后一排个子是逐渐增高的,即后一排最低的个子要>=前一排的所有人 每排排列规则如下: 1.中间m/2+1为该排最高: ...

  5. 1109 Group Photo (25 分)

    1109 Group Photo (25 分) Formation is very important when taking a group photo. Given the rules of fo ...

  6. PAT 1109 Group Photo[仿真][难]

    1109 Group Photo(25 分) Formation is very important when taking a group photo. Given the rules of for ...

  7. 1109 Group Photo

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  8. PAT 1109 Group Photo

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  9. PAT_A1109#Group Photo

    Source: PAT A1109 Group Photo (25 分) Description: Formation is very important when taking a group ph ...

随机推荐

  1. Go - 代码生成工具

    分享两个常用的代码生成工具: gormgen handlergen gormgen 基于 MySQL 数据表结构进行生成 3 个文件: 生成表的 struct 结构体 生成表的 Markdown 文档 ...

  2. 绿色城市之地下综合管廊3D可视化平台

    前言 现阶段,我国绿色城市建设发展正在热火朝天的进行,面对迅速城镇化建设导致的城市病,需要不断寻求足以丰富城市的资源,以此实现城市绿色化智能化发展,比如改造地下管廊.路灯等城市基础设施. 地下综合管廊 ...

  3. Django 自定义标签与过滤器报错 No module named 'templatetags'

    Django 自定义标签与过滤器报错 按照网上的教程如果想使用自定义的标签与过滤器就得往settings.py中添加下列数据 TEMPLATES = [ { 'BACKEND': 'django.te ...

  4. POJ-2253(最短路变形+dijikstra算法+求解所有路径中所有最长边中的一个最小值)

    frogger POJ-2253 这题的代码特别像prim求解最小生成树的代码,其实两者本来也很像. 这里的d数组不再维护的起点到该点的最短距离了,而是路径中的最长距离. #include<io ...

  5. pytorch(10)transform模块(进阶)

    图像变换 Pad 对图片边缘进行填充 transforms.Pad(padding,fill=0,padding_mode='constant') padding:设置填充大小,(a,b,c,d)左上 ...

  6. JAVA多线程与锁机制

    JAVA多线程与锁机制 1 关于Synchronized和lock synchronized是Java的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码 ...

  7. MacOS如何调整JD-GUI反编译工具字体大小

    how to change the fontsize of JD-GUI in MacOS? MacOS如何调整JD-GUI反编译工具字体大小? 问题描述 JD-GUI是一款比较好用的反编译工具,不小 ...

  8. CVE-2017-10271 XMLDecoder 反序列化

    漏洞描述:WebLogic的 WLS Security组件对外提供webservice服务,其中使用了XMLDecoder来解析用户传入的XML数据,在解析的过程中出现反序列化漏洞,可以构造请求对运行 ...

  9. External Libraries中没有Maven的jar包的原因(已解决)

    **深坑!** ## External Libraries中没有Maven的jar包的原因(已解决) 2021年3月1日 --- 搭建一个新项目 IDEA 从 Git 上拉 拉去Maven项目然后 m ...

  10. canvas-修改图片亮度

    canvas操作-修改图片亮度 目录 canvas操作-修改图片亮度 图片亮度的概念 下面用ps截图举一个例子: 调整图片亮度的方案 实现方案一 从RGB到HSV的转换 转换的公式 javascrip ...