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. 《深入理解Java虚拟机:JVM高级特性与最佳实践》读书笔记

    第一部分 走进Java 一.走进Java 1.概述 java广泛应用于嵌入式系统.移动终端.企业服务器.大型机等各种场合,摆脱了硬件平台的束缚,实现了“一次编写,到处运行”的理想 2.java技术体系 ...

  2. java_完数

    题目内容: 一个正整数的因子是所有可以整除它的正整数.而一个数如果恰好等于除它本身外的因子之和,这个数就称为完数.例如6=1+2+3(6的因子是1,2,3). 现在,你要写一个程序,读入两个正整数n和 ...

  3. Java_多项式加法

    题目内容: 一个多项式可以表达为x的各次幂与系数乘积的和,比如: 2x6+3x5+12x3+6x+20 现在,你的程序要读入两个多项式,然后输出这两个多项式的和,也就是把对应的幂上的系数相加然后输出. ...

  4. 自定义SharePoint2013 master page

    SharePoint uses templates to define and render the pages that a site displays. The structure of a Sh ...

  5. Django Rest framework 之 认证

    django rest framework 官网 django rest framework 之 认证(一) django rest framework 之 权限(二) django rest fra ...

  6. 终端复用软件 tmux

    Tmux是一个优秀的终端复用软件,类似GNU Screen,但来自于OpenBSD,采用BSD授权. Tmux 所使用的快捷键前缀默认是组合键 Ctrl-b(同时按下 Ctrl 键和 b 键). 技巧 ...

  7. DOM事件-调用函数

    通过调用函数改变其内容: <!DOCTYPE html> <html lang="en" dir="ltr"> <head> ...

  8. Runtime和Process

    private void runByshcommand(String command) { try { System.out.println("开始执行命令....."); Pro ...

  9. loadrunner 场景设计-添加Windows Resources计数器

    场景设计-添加Windows Resources计数器 by:授客 QQ:1033553122 目的 监控要测试的windows服务器的资源使用情况 步骤 1.添加视图,方法双击.拖动左侧的Windo ...

  10. Quill编辑器IOS下无法获取焦点的解决方法

    造成Quill-Editor无法获取焦点的大部分原因是Css的问题,罪魁祸首: *{ -webkit-user-select:none; } ios下直接造成无法获取焦点. 解决方法,覆盖以上css设 ...