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. Python3 系列之 编程规范篇

    编码规范 编码 如无特殊情况, 文件一律使用 UTF-8 编码 如无特殊情况, 文件头部必须加入 #--coding:utf-8-- 标识 缩进 统一使用 4 个空格进行缩进 引号 自然语言 使用双引 ...

  2. Django Rest Framework之用户频率/访问次数限制

    内置接口代码基本结构 settings.py: REST_FRAMEWORK = { 'DEFAULT_THROTTLE_CLASSES':['api.utils.mythrottle.UserThr ...

  3. C# Select

  4. Python全栈学习_day010作业

    1,继续整理函数相关知识点,写博客. 2,写函数,接收n个数字,求这些参数数字的和.(动态传参)def MySum(*args): sum = 0 for i in range(len(args)): ...

  5. 【工具相关】Web-Sublime Text2-安装 Package Control

    一,打开Sublime text2---->Preferences--->若Package Settings,Package Control,没有的话,就需要安装Package Contr ...

  6. Tars --- Hello World

    服务端开发 1,创建一个 webapp maven 项目,pom.xml 导入依赖 <dependency> <groupId>com.tencent.tars</gro ...

  7. 安卓开发_浅谈WebView(转)

    ,有一个功能需要在APP中调用网站 百度了一下,发现需要用WebView来实现 实现方法很容易,我就不在这里写一遍了 ,直接转一下我学习的内容吧 原创作品,允许转载,转载时请务必以超链接形式标明文章  ...

  8. 手把手教你撸一个简易的 webpack

    背景 随着前端复杂度的不断提升,诞生出很多打包工具,比如最先的grunt,gulp.到后来的webpack和Parcel.但是目前很多脚手架工具,比如vue-cli已经帮我们集成了一些构建工具的使用. ...

  9. csdn中使用git的一些注意事项---免得走弯路

    csdn中使用git必须的条件(windows系统下): 1.本机当前登录用户文件夹下必须有.ssh隐藏文件,并且这个文件中必须有用git bash中用命令生成的密钥文件:id_rsa  id_rsa ...

  10. Fedora 29 查看 rpm 包 依赖性 以 libconfig 为例

    查看依赖性方法:# rpmrepater会向用户显示已安装包的列表,你可以使用上/下箭头来滚动屏幕# 可以在指定包上使用"r"键来显示其依赖关系,循环在指定包上按下"r& ...