Source:

PAT A1109 Group Photo (25 分)

Description:

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

Keys:

  • 简单模拟

Attention:

  • 各行的站位,就是按照从第2高到最矮,依次进栈,入队,进栈,入队...,最高者插在栈和队之间,这样理解起来就很容易了

Code:

 /*
Data: 2019-08-18 20:17:26
Problem: PAT_A1109#Group Photo
AC: 38:15 题目大意:
拍集体照,要求如下:
各行人数为N/K,多余的放在最后一行
各列身高依次递增
各行最高的人站在中间(m/2+1)
各行其余的人,按照身高从大到小,依次站在中间的左,右,左,右...
输入:
第一行给出,总人数N,行数K
接下来N行,姓名,身高
输出:
最后一行的姓名,
...
第一行的姓名;
*/
#include<cstdio>
#include<string>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
const int M=1e4+;
string line[M];
map<string,int> mp; bool cmp(string s1, string s2)
{
if(mp[s1] != mp[s2])
return mp[s1] > mp[s2];
else
return s1 < s2;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,k,high;
scanf("%d%d", &n,&k);
for(int i=; i<n; i++)
{
cin >> line[i] >> high;
mp[line[i]]=high;
}
sort(line,line+n,cmp);
for(int i=; i<k; i++)
{
int st=i*(n/k)+(i==?:n%k);
int ln=n/k+(i==?n%k:);
stack<string> l;
queue<string> r;
vector<string> ans;
for(int j=; j<ln; j++)
{
if(j%==)
r.push(line[j+st]);
else
l.push(line[j+st]);
}
while(!l.empty())
{
ans.push_back(l.top());
l.pop();
}
while(!r.empty())
{
ans.push_back(r.front());
r.pop();
}
for(int j=; j<ans.size(); j++)
printf("%s%c", ans[j].c_str(),j==ans.size()-?'\n':' ');
} return ;
}

PAT_A1109#Group Photo的更多相关文章

  1. A1109. Group Photo

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

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

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

  3. 1109 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 分)

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

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

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

  6. 1109. Group Photo (25)

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

  7. PAT 1109 Group Photo

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

  8. PAT甲级——A1109 Group Photo【25】

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

  9. 1109 Group Photo (25分)

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

随机推荐

  1. 表现与数据分离;前台MVC

    无意间看到一个web前端招聘要求:表现与数据分离 这名词对我非常陌生,我就去百度了下 由于有各种莫名其妙的需求,所以才会出现我们前端MVC这样的莫名其妙的东西... 我们的html就是model,我们 ...

  2. oc44--多对象内存管理

    // Room.h #import <Foundation/Foundation.h> @interface Room : NSObject @property int no;// 房间号 ...

  3. 【概念的辨异】—— ISO C 与 POSIX C(C standard library 与 C POSIX library)

    ISO C 表示 C Standard Library,也就是 C 标准库. 二者的主要区别在于: POSIX 是 C 标准库的超集(也即是从内容上,C 标准库是 POSIX 库的一部分,POSIX ...

  4. B1192 [HNOI2006]超级英雄Hero 二分图匹配

    先检讨一下,前一段时间开学,做题懒得发博客,也不总结...现在捡起来. 这个题一看是裸的二分图匹配,但是仔细一看还有一些区别,就是必须要连续的连接,否则直接退出.因为前一道题答不出来的话后面的题就没有 ...

  5. AAC帧格式及编码介绍

    参考资料: AAC以adts格式封装的分析:http://wenku.baidu.com/view/45c755fd910ef12d2af9e74c.html aac编码介绍:http://wenku ...

  6. RTP协议分析和详解

    一.RTP协议分析 第1章.     RTP概述 1.1.  RTP是什么 RTP全名是Real-time Transport Protocol(实时传输协议).它是IETF提出的一个标准,对应的RF ...

  7. Blender插件初始化范例

    目标 [x] 总结Blender插件初始化范例 总结 插件模板 Blender内部插件实现方式模板功能总结如下: 定义了子模块重加载方式 定义了批量加载子模块的方式 插件注册函数 插件注销函数 模块总 ...

  8. Halcon学习笔记之支持向量机(二)

    例程:classify_halogen_bulbs.hdev 在Halcon中模式匹配最成熟最常用的方式该署支持向量机了,在本例程中展示了使用支持向量机对卤素灯的质量检测方法.通过这个案例,相信大家可 ...

  9. 【Codeforces】Codeforces Round #374 (Div. 2) -- C. Journey (DP)

    C. Journey time limit per test3 seconds memory limit per test256 megabytes inputstandard input outpu ...

  10. [转载]Android平台第三方应用分享到微信开发

    一.申请APPID 微信公共平台和微博分享一样,也需要申请一个ID,来作为调起微信.分享到微信的唯一标识. 申请微信APPID可以到微信平台http://open.weixin.qq.com/app/ ...