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 (≤10​4​​), 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

 #include <stdio.h>
#include <algorithm>
#include <set>
#include <string.h>
#include <vector>
#include <math.h>
#include <queue>
#include <iostream>
#include <string>
using namespace std;
const int maxn = ;
int n,k;
string res[][maxn];
struct node{
string id;
int h;
};
bool cmp(node n1,node n2){
return n1.h==n2.h?n1.id<n2.id:n1.h>n2.h;
}
vector<node> v;
int main(){
scanf("%d %d",&n,&k);
for(int i=;i<=n;i++){
node tmp;
cin>>tmp.id>>tmp.h;
v.push_back(tmp);
}
sort(v.begin(),v.end(),cmp);
int num = n/k;
int left = n - (k-)*num;
int pos=left/+;
int offset=,flag=;
for(int i=;i<left;i++){
res[][pos]=v[i].id;
offset++;
flag*=-;
pos = pos+offset*flag;
}
int index=left;
for(int i=;i<k;i++){
pos=num/+;
offset=,flag=;
for(int j=;j<num;j++){
res[i][pos]=v[index++].id;
offset++;
flag*=-;
pos = pos+offset*flag;
}
}
for(int i=;i<=left;i++){
printf("%s%s",res[][i].c_str(),i==left?"\n":" ");
}
for(int i=;i<k;i++){
for(int j=;j<=num;j++){
printf("%s%s",res[i][j].c_str(),j==num?"\n":" ");
}
}
}

注意点:按题目实现排序后一个个插进去就好了,半小时多点ac,还行吧

PAT A1109 Group Photo (25 分)——排序的更多相关文章

  1. 1109 Group Photo (25分)

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

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

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

  3. PTA PAT排名汇总(25 分)

    PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...

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

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

  5. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

    1070 Mooncake (25 分)   Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...

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

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

  7. A1109. Group Photo

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

  8. PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

    1029 Median (25 分)   Given an increasing sequence S of N integers, the median is the number at the m ...

  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. 中国MOOC_面向对象程序设计——Java语言_第4章 继承与多态_第4周编程题_将MP3媒体类型存放进Database

    本周我们介绍了以继承方式实现的媒体资料库,在课程代码实现的基础上,请实现一个表达MP3的媒体类型,能和CD.DVD一样存放进这个Database.请提交这个MP3类的代码.如果你认为为了能存放MP3, ...

  2. Hybris IMPEX.

    1.Impex是基于java Model的一种面向对象的数据操作手段,因此写impex代码前需要理清java Model之间的依赖关系.   2.基本语法:mode type[modifier=val ...

  3. Linux常用基本命令:三剑客命令之-awk基础用法

    awk是一个超级强大的文本格式化处理工具,他与grep, sed命令被成为linux 三剑客命令 三剑客命令的特点: grep:只要用来匹配和查找文本 sed: 编辑匹配到文本 awk: 格式化文本, ...

  4. Django&Flask区别

    Flask Flask 本身只有一个内核,几乎所有的功能都需要用第三方的扩展来实现. Flask 没有默认使用的数据库,默认依赖两个外部库:Jinja2 模板引擎和 WSGI 工具箱(采用的时 Wer ...

  5. SP8093 JZPGYZ - Sevenk Love Oimaster(广义后缀自动机)

    题意 题目链接 Sol 广义后缀自动机板子题..和BZOJ串那个题很像 首先建出询问串的SAM,然后统计一下每个节点被多少个串包含 最后直接拿询问串上去跑就行了 #include<bits/st ...

  6. 能用HTML/CSS解决的问题,就不要用JS

    原因:简单. 简单就意味着更快的开发速度,更小的维护成本,同时往往具有更好的体验. 一,导航高亮 效果图: 代码: <!DOCTYPE html> <html lang=" ...

  7. FI / CO 配置步骤清单

    一.FI配置 01. 创建公司代码:企业结构-定义-财务会计-编辑/复制/删除/检查公司代码.编辑公司OX02 02. 创建会计科目表 OB13 03. 定义会计年度变式 OB29 04. 创建信贷控 ...

  8. Windows 10修复

    [以管理员运行如下命令]: 1.sfc /scannow 命令将扫描所有受保护的系统文件,并用位于 %WinDir%\System32\dllcache 的压缩文件夹中的缓存副本替换损坏的文件. 2. ...

  9. linux-nc命令介绍

    转载:https://www.cnblogs.com/nmap/p/6148306.html

  10. 不用Visual Studio,5分钟轻松实现一张报表

    常规的报表设计,如RDLC.水晶报表等,需要安装Visual Studio,通过VS提供的报表设计界面来设计报表,通过VS设计报表对.NET开发者而言非常方便,但是对于非开发人员,要安装4G的一个VS ...