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. (3)Microsoft office Word 2013版本操作入门_段落设定

    1.查看文件: 打开word查看左下角 会显示 word一共有多少页,当前第几页,共多少字等,如下图所示 2.word快速翻页: Ctrl+PageDown  向下翻页, Ctrl+PageUp 向上 ...

  2. Java学习笔记之——Java介绍

    1.Java体系: JavaSE:标准版,其他两个体系的基础 JavaEE:企业版 JavaME:微型版,适用于消费类型的微型设备 2.Java三大特性:封装.继承.多态 3.Java的特点:面向对象 ...

  3. 关于eclipse项目的x号报错的一些问题

    有些时候项目中并未有什么问题 但项目前会有一个X号报错且无法运行项目 我们不妨从jre和Tomcat的一些配置中找原因 1,首先查看jre的安装是否正确,可以看见并未出错 ,如果有问题,重新导入一下即 ...

  4. HTML暗黑料理之a标签执行请求不跳转页面

    不是被逼无奈不建议用这HTML暗黑料理. <iframe id="></iframe> <a class="large green button&qu ...

  5. 【Mybatis】一对多实例

    ①创建数据库和表,数据库为mytest,表为teacher和student DROP TABLE IF EXISTS teacher; DROP TABLE IF EXISTS student; CR ...

  6. vue.js及项目实战[笔记]— 04 axios

    一. axios 1. 基本使用 axios.method('url',[,...data],options) .then(function(res){ }) .catch(function(err) ...

  7. linux-nc命令介绍

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

  8. IO事件驱动模型

    1:IO事件驱动模型简介 通常,我们写服务器处理模型的程序时,有以下几种模型: (1)每收到一个请求,创建一个新的进程,来处理该请求: (2)每收到一个请求,创建一个新的线程,来处理该请求: (3)每 ...

  9. 进程管理-PV操作

    1.临界资源:诸进程需要互斥方式对其进行共享的资源. 2.临界区:每个进程中访问临界资源的那段代码. 3.信号量:一种特殊的变量.

  10. JS获取当前星期几的简易写法

    var str = "今天是星期" + "日一二三四五六".charAt(new Date().getDay()); mark在此,方便日后复制 原文https ...