1109 Group Photo (25 分)
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 (≤104), 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
分析:一开始想的是把左右两边都排序一次,但感觉较复杂且肯定耗时大,后来还是参考了柳神的代码。。思路清晰。。
具体做法:每排一个大循环,最大的先插入,两侧的按序一个个插入,插入过程中i=i+2,注意跳出条件。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
struct student{
string name;
int h;
};
bool cmp(student a,student b){
if(a.h!=b.h) return a.h>b.h;
else if(a.name!=b.name) return a.name<b.name;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("1.txt", "r", stdin);
#endif
int n,k,i,j,m;
cin>>n>>k;
vector<student> stu(n);
;i<n;i++){
cin>>stu[i].name>>stu[i].h;
}
sort(stu.begin(),stu.end(),cmp);
,row=k;
while(row){
if(row==k){
m=n/k+n%k;
}
else m=n/k;
vector<string> ans(m);
ans[m/]=stu[t].name;
j=m/-;
;i<t+m;i=i+){
ans[j--]=stu[i].name;
}
j=m/+;
;i<t+m;i=i+){
ans[j++]=stu[i].name;
}
cout<<ans[];
;i<m;i++){
cout<<" "<<ans[i];
}
cout<<endl;
t=t+m;
row--;
}
;
}
1109 Group Photo (25 分)的更多相关文章
- 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 (25分)(模拟)
题意: 输入两个整数N和K(N<=1e4,K<=10),分别表示人数和行数,接着输入N行每行包括学生的姓名(八位无空格字母且唯一)和身高([30,300]的整数).按照身高逆序,姓名字典序 ...
- 1109. Group Photo (25)
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
- 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 1109 Group Photo[仿真][难]
1109 Group Photo(25 分) Formation is very important when taking a group photo. Given the rules of for ...
- 1109 Group Photo
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
Source: PAT A1109 Group Photo (25 分) Description: Formation is very important when taking a group ph ...
随机推荐
- 使用 PHP 框架 Yii 访问 MS SQL 的尝试
今天尝试在 Wamp 环境下访问 MS SQL 数据库,有以下心得.使用 PHP 5.3 + pdo_odbc + Yii 1.1 框架,以下两个代码段可以成功. 1.直接使用 PDO 方式,可以获取 ...
- Kali Liunx 2.0震撼来袭(附下载地址、新特性和更新日志)
Kali 2.0昨天已经在BlackHat 2015 USA上正式发布了.无论是粉丝们还是Kali官方都对这个2.0版本抱有极大的期待和热情.这是第一个基于Debian Jessie的Kali版本,此 ...
- 掌握所有编程语言不是梦-转自csdn
还在讨论哪种编程语言最具价值?还在头疼怎么快速掌握新的语言?本文给出了一劳永逸的办法:掌握所有编程语言不是梦. 以下为译文: 我想在这里讨论的不是如何掌握一门编程语言,而是掌握所有的编程语言. 许多初 ...
- Windows下C++删除清除map
清除单map(非嵌套map) #include<map> #include<string> #include<iostream> using namespace s ...
- caffe安装编译问题-ImportError: No module named skimage.io
问题描述 >>> import caffe Traceback (most recent call last): File , in <module> File , in ...
- 如何创建一个基于 .NET Core 3 的 WPF 项目
在 Connect(); 2018 大会上,微软发布了 .NET Core 3 Preview,以及基于 .NET Core 3 的 WPF:同时还发布了 Visual Studio 2019 预览版 ...
- 【洛谷P1462】【二分+堆优化dij】
题目描述 在艾泽拉斯,有n个城市.编号为1,2,3,...,n. 城市之间有m条双向的公路,连接着两个城市,从某个城市到另一个城市,会遭到联盟的攻击,进而损失一定的血量. 每次经过一个城市,都会被收取 ...
- C语言命令行解析函数:getopt/getopt_long
命令行工具下的参数选项有两种,长选项和短选项.短选项以-开头,后面跟单个字母:长选项以--开头,后面可跟多个字母. 一. getopt() 1.功能:解析命令行短选项参数 2.函数原型: #inclu ...
- [DL] 基于theano.tensor.dot的逻辑回归代码中的SGD部分的疑问探幽
在Hinton的教程中, 使用Python的theano库搭建的CNN是其中重要一环, 而其中的所谓的SGD - stochastic gradient descend算法又是如何实现的呢? 看下面源 ...
- PIG之 Hadoop 2.7.4 + pig-0.17.0 安装
首先: 参考 http://blog.csdn.net/zhang123456456/article/details/77621487 搭建好hadoop集群. 然后,在master节点安装pig. ...