PAT_A1109#Group Photo
Source:
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的更多相关文章
- A1109. Group Photo
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 N peop ...
- 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 ...
- PAT甲级——A1109 Group Photo【25】
Formation is very important when taking a group photo. Given the rules of forming K rows with Npeopl ...
- 1109 Group Photo (25分)
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
随机推荐
- A java code
With the help of LiJun I got a piece of JAVA code. With this code, I can do below things like connec ...
- 菜鸟学Java(二十二)——又一次认识泛型
泛型是Java SE 1.5的新特性,泛型的本质是參数化类型,也就是说所操作的数据类型被指定为一个參数.这样的參数类型能够用在类.接口和方法的创建中,分别称为泛型类.泛型接口.泛型方法. Java语言 ...
- 查找存在某字符的文件列表,不包括svn文件
find . ! -wholename '*.svn*' -print | xargs grep "img" | awk -F ':.' '{print $1}' | uniq
- Sublime text 神器小记
曾经一直使用的都是Editplus,一直感觉都是非常不错的. 可是近期接触到sublime text,我就fall in love with it. Sublime 的意思是"顶峰,高尚,非 ...
- bzoj5130: [Lydsy1712月赛]字符串的周期
这道题很有意思啊. 字符串循环节用KMP(手推一下) 假如是26^12肯定很不滋磁 但是可以发现ABA和BCB和BAB这些都是等价的 那就把最小的拿出来搞再乘个排列数就好了 #include<c ...
- hdu1150——最小点覆盖
As we all know, machine scheduling is a very classical problem in computer science and has been stud ...
- 利用递归分割(Split)字符串
利用递归分割(Split)字符串 SqlServer 递归 工作需要将表里的某个字段分割之后再插入到另一个表中,其实数据量不大,直接用游标一行一行的取,再利用循环来分割之后再实现数据的插入应该可以直接 ...
- HTTP权威协议笔记-9.Web机器人
经过整个春节的放肆,终于回归了,说实话,春节真心比上班累. 9.1 爬虫及爬行方式 (1) 爬虫:Web爬虫是一种机器人,他们会递归性的对各种信息Web站点进行遍历. (2) 爬行方式:Web机器人会 ...
- PCB 奥宝LDI 输出正负片转换关系
今天继续对P2 奥宝LDI改造,在文件输出的时候遇到了一个正负片转换问题,研究了半天一直没有得到解决, 回来后前前后后整理今天参数输出与输出的关系,最终还梳理清楚了, 今天小结:一项技术只要用心去研究 ...
- html5小知识点
1.兼容性问题: 对于不支持H5标签的浏览器,可以使用javascript来解决他们.然后在样式表中对这些标签定义一下默认的display:block. 采用第三方库:html5shiv.js < ...