PAT甲级——A1109 Group Photo【25】
Formation is very important when taking a group photo. Given the rules of forming K rows with Npeople 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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Node
{
string name;
int height;
}node;
bool cmp(Node a, Node b)
{
if (a.height == b.height)
return a.name < b.name;
else
return a.height > b.height;
}
int main()
{
int n, m, k;
cin >> n >> k;
m = n / k;
vector<string>*v = new vector<string>[k];
vector<Node>people;
for (int i = ; i < k; ++i)
{
if (i == && n%k != )
v[].resize(m + n % k);
else
v[i].resize(m);
}
while (n--)
{
cin >> node.name >> node.height;
people.push_back(node);
}
sort(people.begin(), people.end(), cmp);
int t = ;
for (int i = ; i < k; ++i)
{
m = v[i].size();
int mid = m / , left = mid - , right = mid + ;
v[i][mid] = people[t++].name;
while (left >= || right < m)
{
if (left >= )
v[i][left--] = people[t++].name;
if (right < m)
v[i][right++] = people[t++].name;
}
}
for (int i = ; i < k; ++i)
{
for (int j = ; j < v[i].size(); ++j)
cout << v[i][j] << (j == v[i].size() - ? "" : " ");
cout << endl;
}
return ;
}
PAT甲级——A1109 Group Photo【25】的更多相关文章
- PAT A1109 Group Photo (25 分)——排序
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 ...
- A1109. 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 ...
- 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)
题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...
- 【PAT甲级】1109 Group Photo (25分)(模拟)
题意: 输入两个整数N和K(N<=1e4,K<=10),分别表示人数和行数,接着输入N行每行包括学生的姓名(八位无空格字母且唯一)和身高([30,300]的整数).按照身高逆序,姓名字典序 ...
- PAT (Advanced Level) 1109. Group Photo (25)
简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT甲题题解-1109. Group Photo (25)-(模拟拍照排队)
题意:n个人,要拍成k行排队,每行 n/k人,多余的都在最后一排. 从第一排到最后一排个子是逐渐增高的,即后一排最低的个子要>=前一排的所有人 每排排列规则如下: 1.中间m/2+1为该排最高: ...
- PAT甲级 1122. Hamiltonian Cycle (25)
1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...
随机推荐
- PHP ftp_raw() 函数
定义和用法 ftp_raw() 函数向 FTP 服务器发送一个 raw 命令. 语法 ftp_raw(ftp_connection,command) 参数 描述 ftp_connection 必需.规 ...
- Delphi中绘制圆角矩形的窗体
制作圆角矩形的窗体: 01.procedure TPortForm.FormCreate(Sender: Tobject); 02.var hr :thandle; 03.begin 04.hr:=c ...
- Spring-Security (学习记录六)--采用ehcache缓存UserDetails
目录 1. spring-security提供了缓存UserDetails的UserDetailsService实现类. 2. 通过配置来完成缓存 UserDetails (使用ehcache) 3. ...
- RoadFlow开源工作流源码-项目架构分析
项目文件结构: 很明了一个标准的三层架构的系统. 表示层:Web 业务层:Business 数据访问层:Data 另外存在缓存层:Cache缓存 增加公共使用类库:Utility 下面以一个实例(系统 ...
- Python3 From Zero——{最初的意识:006~数据编码与处理}
一.读写CSV数据: #!/usr/bin/env python3 #-*- coding=utf8 -*- import csv with open('kxtx.csv', 'rt') as f: ...
- servlet的xml配置详解
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns ...
- WebApi 如何 优雅的 对 输入输出 解密加密
原文:WebApi 如何 优雅的 对 输入输出 解密加密 这不是变态的想法, 这只是对现实需求的转化. 因为有密文, 所以本文不适用于浏览器到服务端的数据交换; 只适用于服务端到服务端的数据传输. 用 ...
- 【第五周读书笔记】我是一只IT小小鸟
读了第一个同学的自述,我印象最深的就是一些高分同学,只是机械性地背诵知识点,然后不停刷题,只是为了拿一个高分,然而他们对学科的一些基本概念都没有掌握牢靠.高分,并不代表学的就好.学得好不仅仅要牢靠掌握 ...
- Django ORM 之F、Q查询与事务
返回ORM目录 Django ORM 内容目录 一.F.Q查询 二.事务 三.only与defer 一.F.Q查询 """ Product表中的数据: 1 橡皮 2 20 ...
- java运行字符串代码
本文链接:https://blog.csdn.net/junlong750/article/details/50945883