A1109. Group Photo
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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
typedef struct{
string name;
int high;
}info;
info people[], line[];
bool cmp(info a, info b){
if(a.high != b.high)
return a.high > b.high;
else return a.name < b.name;
}
int main(){
int N, K;
cin >> N >> K;
for(int i = ; i < N; i++){
cin >> people[i].name >> people[i].high;
}
sort(people, people + N, cmp);
int lineSize = N / K;
int lastSize = N - lineSize * (K - );
int index = ;
for(int i = ; i < K; i++){
int len = i == ? lastSize : lineSize;
int mid = len / ;
int left = mid - , right = mid + ;
line[mid] = people[index++];
while(left >= && right < len){
line[left--] = people[index++];
line[right++] = people[index++];
}
if(left == )
line[left--] = people[index++];
for(int j = ; j < len - ; j++){
cout << line[j].name << " ";
}
cout << line[len - ].name << "\n";
}
cin >> N;
return ;
}
总结:
1、题意:按照集体照像的原则排队,每一排站N / K个人,最后一排如果有多的则都站最后一排。每一排都比前一排要高。对于每一排,个子最高的站中间,然后次高的站他的左边,第三高的站他的右边。如此从中间向两边一左一右的安排。 如果有一样高的,则按照姓名字典序排序。
2、本题可以先整体从高到低排序。然后逐行安排,每排完一行就输出该行。使用index作为整体数组的指针,每排完一个人就+1。对于每一排,从中间向两边安排即可。
3、最好不要从前往后排,这样需要保存后统一输出,很麻烦。
A1109. Group Photo的更多相关文章
- PAT A1109 Group Photo (25 分)——排序
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
- PAT甲级——A1109 Group Photo【25】
Formation is very important when taking a group photo. Given the rules of forming K rows with Npeopl ...
- PAT_A1109#Group Photo
Source: PAT A1109 Group Photo (25 分) Description: Formation is very important when taking a group ph ...
- 1109 Group Photo
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
- 1109 Group Photo (25 分)
1109 Group Photo (25 分) Formation is very important when taking a group photo. Given the rules of fo ...
- PAT 1109 Group Photo[仿真][难]
1109 Group Photo(25 分) Formation is very important when taking a group photo. Given the rules of for ...
- 1109. Group Photo (25)
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
- PAT 1109 Group Photo
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
- 1109 Group Photo (25分)
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
随机推荐
- java学习之—并归排序
/** * 并归排序 * Create by Administrator * 2018/6/26 0026 * 下午 5:13 **/ public class DArray { private lo ...
- Airflow 使用随笔(内含 TimeZone 和 Backfill 等的详解)
其实怎么部署 airflow 又哪些特性,然后功能又是如何全面都可以在 Reference 的文章里面找到,都不是重点这里就不赘述了. 这里重点谈一下我在部署完成仔细阅读文档之后觉得可以总结的一些东 ...
- 在windows 7上安装TensorFlow
TensorFlow是一个开源软件库,用于各种感知和语言理解任务的机器学习.目前被50个团队用于研究和生产许多Google商业产品,如语音识别.Gmail.Google 相册和搜索,其中许多产品曾使用 ...
- linux pstree命令
pstree命令可以使进程以tree的形式显示 pstree -ssystemd─┬─UVPHostd───6*[{UVPHostd}] ├─acpid ├─2*[agetty] ├─crond ├─ ...
- StatefulSet
StatefulSet: 1.稳点且唯一的网络标识符 2.稳点且持久的存储 3.有序.平滑的部署和扩展 4.有序.平滑的删除和终止 5.有序的滚动更新 三个组件组成:headless(无头服务) ...
- MySQL系列:数据表基本操作(2)
1. 指定数据库 mysql> use portal; 2. 数据库表基本操作 2.1 查看数据表 mysql> show tables; +------------------+ | T ...
- ubuntu 有些软件中不能输入中文
如果Ubuntu设定的是英文语言,在各种软件例如wps等中很有可能就不能输入中文.这种情况,我们的解决方案是,把中文输入法加到软件的启动文件中,如何加呢?把下面内容加进去就可以解决: export X ...
- LODOP设置判断后执行哪个
LODOP的语句是普通的语句,可以通过JS判断确定要执行哪个,或通过循环循环执行一些语句.如果需要执行某些打印项在哪些条件下不打印,不需要通过代码删除打印项,类似LODOP.SET_PRINT_STY ...
- caffe2学习
https://www.jianshu.com/p/50bf3bd4e3d0 知乎专栏 https://zhuanlan.zhihu.com/kingbob
- MyBatis实操进阶版(一)
MyBatis实操进阶版(一) 目前而言,持久层框架中,在业务实现灵活性上,无可出MyBatis之右者.具体原因,后续将逐步展开 ResultMap元素的设置 配置文件中,ResultMap元素的作用 ...