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】的更多相关文章

  1. PAT A1109 Group Photo (25 分)——排序

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

  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. A1109. Group Photo

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

  4. 1109. Group Photo (25)

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

  5. 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

    题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...

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

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

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

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

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

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

  9. PAT甲级 1122. Hamiltonian Cycle (25)

    1122. Hamiltonian Cycle (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

随机推荐

  1. Centos 添加 sudo 用户

    说明以下的 <username>字样 是 用户名 新增用户 # 终端命令 useradd <username> 举例: useradd <username> -s ...

  2. 文档 所有空格变为Tab

    遗憾的是记事本.word没有这个功能... 可以生成exe #include <cstdio> #include <cstdlib> #include <cmath> ...

  3. C++:查找字符串字串并替换

    string a;/////指定串,可根据要求替换 string b;////要查找的串,可根据要求替换 string c; cin>>a>>b>>c; int p ...

  4. Java——package和import关键字

    1.8 package和import关键字 1.8.1 package 包其实就是目录,特别是项目比较大,java 文件特别多的情况下,我们应该分目录管理,在java 中称为分包管理,包名称通常采用小 ...

  5. thinkphp 应用模式

    应用模式提供了对核心框架进行改造的机会,可以让你的应用适应更多的环境和不同的要求. 每个应用模式有自己的模式定义文件,用于配置当前模式需要加载的核心文件和配置文件,以及别名定义.行为扩展定义等等.根据 ...

  6. JavaScript中的函数柯里化与反柯里化

    一.柯里化定义 在计算机科学中,柯里化是把 接受多个参数的函数 变换成 接受一个单一参数(最初函数的第一个参数)的函数 并且返回 接受余下参数且返回结果的新函数的技术 高阶函数 高阶函数是实现柯里化的 ...

  7. NOIp2018集训test-9-17(am)

    这是一套去年在长沙考过的题,但是我当时就没理清楚+没写题解(我以前很多博客都写得跟shi一样,完全没有意义,看到就想打当时的我),所以又考得稀烂. T1.star way to heaven 容易想到 ...

  8. sqlserver 调优(三)

    用户数据库质疑状态处理(可能由于机房断电,数据库服务器异常重启后,导致个别数据库状态质疑): --修复数据库(置疑) -- xxxDB 为需要修复的数据库的名称 ALTER DATABASE xxxD ...

  9. ElasticSearch 索引查询使用指南

    1.检测集群是否健康,我们通常用下面的命令.确保9200端口号可用: http://localhost:9200/_cat/health?v 或者 http://localhost:9200/_clu ...

  10. 面向对象编程思想(OOP)总结

    本文我将从面向对象编程思想是如何解决软件开发中各种疑难问题的角度,来讲述我们面向对象编程思想的理解,梳理面向对象四大基本特性.七大设计原则和23种设计模式之间的关系. 软件开发中疑难问题: 软件复杂庞 ...