1109 Group Photo
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.
思路:
先排序
John Tim Amy, Eva Ann Mike, Bob Nick Tom Joe
man_in_row = n/k; 以每行为单位进行排列,从个子矮的行开始排
注意* 最初排序的方式应该是身高升序,名降序:这样才能保证在每行中进行排列的时候,先排的是个子高的,身高相同时是名字典序小的
* 对每行进行排列,对每行的循环应该这样写 for(; ptr+man_in_row*2<=n; ptr+=man_in_row)!!
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
using namespace std;
const int maxn = ; struct mantype{
string name;
int height;
};
struct mantype manList[]; bool cmp(struct mantype m1,struct mantype m2){
if(m1.height==m2.height){
return m1.name>m2.name;
}
return m1.height<m2.height;
} int main(){
int n,k; scanf("%d %d",&n,&k);
string tmps; int height;
for(int i=;i<n;i++){
cin >> manList[i].name >> manList[i].height; // !!
}
sort(manList, manList+n, cmp); /*for(int i=0;i<n;i++){
cout << manList[i].name << " ";
}*/ string matrix[][];
int man_in_row = n/k; int ptr = ; int lv=;
int t,i;
for(; ptr+man_in_row*<=n; ptr+=man_in_row){ // !!
int mid = man_in_row/+;
matrix[lv][mid] = manList[ptr+man_in_row-].name;
t=;
for(i=ptr+man_in_row-;i>=ptr;i-=){
matrix[lv][mid-t] = manList[i].name;
if(i->=ptr) matrix[lv][mid+t] = manList[i-].name;
t++;
}
lv++;
}
int remain = (n-ptr);
int mid = remain/+;
matrix[lv][mid] = manList[n-].name;
t=;
for(i=n-;i>=ptr;i-=){
matrix[lv][mid-t] = manList[i].name;
if(i->=ptr) matrix[lv][mid+t] = manList[i-].name;
t++;
} //printf("\n------\n"); for(int j=;j<=remain;j++){
if(j!=) printf(" ");
cout<<matrix[lv][j];
}
printf("\n");
for(int i=lv-;i>=;i--){
for(int j=;j<=man_in_row;j++){
if(j!=) printf(" ");
cout<<matrix[i][j];
}
printf("\n");
} return ;
}
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
1109 Group Photo的更多相关文章
- 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 ...
- 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 (25分)(模拟)
题意: 输入两个整数N和K(N<=1e4,K<=10),分别表示人数和行数,接着输入N行每行包括学生的姓名(八位无空格字母且唯一)和身高([30,300]的整数).按照身高逆序,姓名字典序 ...
- A1109. Group Photo
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
随机推荐
- YII2中使用RBAC对模块,控制器,方法的权限控制以及规则的使用
在使用YII2中自带的RBAC时,需要先配置config/web.php: return [ // ... 'components' => [ 'authManager' => [ 'cl ...
- centos7下docker1.12.5学习笔记
一.Docker简介 Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相 ...
- 模块二 hashlib模块、configparser模块、logging模块
算法介绍 Python的hashlib提供了常见的摘要算法,如MD5,SHA1等等. 什么是摘要算法呢?摘要算法又称哈希算法.散列算法.它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常 ...
- DevExpress中RibbonControl的简单应用
现在介绍一下DevExpress中的RibbonControl的简单应用,搭建一个拥有Office2010风格的窗体程序. 第一步:打开VS2015,新建一个windows窗体应用程序: 第二步:把需 ...
- 添加exe为windows service服务
[方法一] 一.介绍 srvany.exe是Microsoft Windows Resource Kits工具集的一个实用小工具,用于将EXE程序作为Windows服务运行.srvany是其注册程序的 ...
- Win10传递优化设置技巧
什么是“传递优化缓存” “传递优化”是微软为了加快Windows更新和Microsoft Store应用更新的下载速度,而在Windows10中引入的一种“自组织分布式本地化缓存”设计,可以在用户电脑 ...
- Python的程序入口 __name__属性
python中每个模块都有一个 '__name__' 属性,当其值为 '__main__' 时,表名该模块自身在运行,否则是被引入的. 当一个模块被当做一个整体调用的时候,模块名.__name__ 的 ...
- Eclipse中配置Tomcat服务器并创建标准Web目录
Eclipse创建 Java Web 项目,并生成标准的目录结构 file --> New --> Dynamic Web project 填写 Project name (该名称项目的名 ...
- HashMap 、HashTable、TreeMap、WeakHashMap的区别是什么
Java为数据结构中的映射定义了一个接口java.util.Map,它有4个实现类:HashTable.HashMap.TreeMap.WeakHashMap. HashMap和HashTable的区 ...
- [Jmeter] 用xsltproc生成html格式的报告
1.下载xsltproc 下载地址:ftp://ftp.zlatkovic.com/libxml/libxslt-1.1.26.win32.zip 其中包含我们所需要的xsltproc可执行文件:xs ...