PAT A1109 Group Photo (25 分)——排序
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.
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 分)——排序的更多相关文章
- 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 (25分)(模拟)
题意: 输入两个整数N和K(N<=1e4,K<=10),分别表示人数和行数,接着输入N行每行包括学生的姓名(八位无空格字母且唯一)和身高([30,300]的整数).按照身高逆序,姓名字典序 ...
- PTA PAT排名汇总(25 分)
PAT排名汇总(25 分) 计算机程序设计能力考试(Programming Ability Test,简称PAT)旨在通过统一组织的在线考试及自动评测方法客观地评判考生的算法设计与程序设计实现能力,科 ...
- PAT甲级——A1109 Group Photo【25】
Formation is very important when taking a group photo. Given the rules of forming K rows with Npeopl ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- PAT 1109 Group Photo[仿真][难]
1109 Group Photo(25 分) Formation is very important when taking a group photo. Given the rules of for ...
- A1109. Group Photo
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
- PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- 1109. Group Photo (25)
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
随机推荐
- 我的Java之旅 第五课 JAVA 语言语法 集合
JAVA集合只能存放引用类型的的数据,不能存放基本数据类型,int 可以用 Integer代替. 一.集合接口 1. Iterable<E> 实现这个接口允许对象成为 "fo ...
- MySQL 性能优化--优化数据库结构之优化数据大小
MySQL性能优化--优化数据库结构之优化数据大小 By:授客 QQ:1033553122 尽量减少表占用的磁盘空间.通常,执行查询期间处理表数据时,小表占用更少的内存. 表列 l 尽可能使 ...
- flow-vue.js移动端效果
得益于vue.js和element,以及vue-element-extends在线表格编辑.前后端分离的后端用golang+beego框架,服务器采用腾讯云. vue的自适应做的很好,只要将侧栏加一行 ...
- tab 页形式展现多张报表
业务系统中,很多报表都是沿用之前 EXCEL 的报表样式,原来以 sheet 格式显示的表,客户在 web 端展现的时候也希望也有同样的格式,润乾在实现这种效果和 EXCEL 一样简单灵活,轻松将数据 ...
- retrofit框架接口调用时候报Throwing new exception
最近在开发的时候遇到了一个很坑的问题,在三星6.0手机上请求接口时候报了一个异常:Throwing new exception 'length=1658; index=3248' with unexp ...
- (网页)angular js 终极购物车(转)
转自CSDN: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- [20171213]john破解oracle口令.txt
[20171213]john破解oracle口令.txt --//跟别人讨论的oracle破解问题,我曾经提过不要使用6位字符以下的密码,其实不管那种系统低于6位口令非常容易破解.--//而且orac ...
- ASP.NET MVC从请求到响应发生了什么
*过程描述 当浏览器发出一个http请求后,该请求被UrlRoutingModule截获,UrlRoutingModule根据请求上下文去系统路由表(RouteTable)中匹配,从中获取一个Rout ...
- 洗礼灵魂,修炼python(32)--面向对象编程(2)—进一步认识类
上一篇文章已经看到了如何定义类,但是我想你应该有很多疑惑的吧?最好的学习方法就是不断思考,不断问为什么,不断和已有知识做类比,从中获得理解.那么这一篇博文就是从解惑答疑中进一步认识类. 解惑答疑 我按 ...
- Javascript 高级程序设计--总结【二】
********************** Chapter 6 ********************** 属性: 数据属性: Configurable: 能否通过delete 删除属性,默认 ...