1109 Group Photo
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.
思路:
先排序
John Tim Amy, Eva Ann Mike, Bob Nick Tom Joe
man_in_row = n/k; 以每行为单位进行排列,从个子矮的行开始排
注意* 最初排序的方式应该是身高升序,名降序:这样才能保证在每行中进行排列的时候,先排的是个子高的,身高相同时是名字典序小的
* 对每行进行排列,对每行的循环应该这样写 for(; ptr+man_in_row*2<=n; ptr+=man_in_row)!!
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
using namespace std;
const int maxn = ; struct mantype{
string name;
int height;
};
struct mantype manList[]; bool cmp(struct mantype m1,struct mantype m2){
if(m1.height==m2.height){
return m1.name>m2.name;
}
return m1.height<m2.height;
} int main(){
int n,k; scanf("%d %d",&n,&k);
string tmps; int height;
for(int i=;i<n;i++){
cin >> manList[i].name >> manList[i].height; // !!
}
sort(manList, manList+n, cmp); /*for(int i=0;i<n;i++){
cout << manList[i].name << " ";
}*/ string matrix[][];
int man_in_row = n/k; int ptr = ; int lv=;
int t,i;
for(; ptr+man_in_row*<=n; ptr+=man_in_row){ // !!
int mid = man_in_row/+;
matrix[lv][mid] = manList[ptr+man_in_row-].name;
t=;
for(i=ptr+man_in_row-;i>=ptr;i-=){
matrix[lv][mid-t] = manList[i].name;
if(i->=ptr) matrix[lv][mid+t] = manList[i-].name;
t++;
}
lv++;
}
int remain = (n-ptr);
int mid = remain/+;
matrix[lv][mid] = manList[n-].name;
t=;
for(i=n-;i>=ptr;i-=){
matrix[lv][mid-t] = manList[i].name;
if(i->=ptr) matrix[lv][mid+t] = manList[i-].name;
t++;
} //printf("\n------\n"); for(int j=;j<=remain;j++){
if(j!=) printf(" ");
cout<<matrix[lv][j];
}
printf("\n");
for(int i=lv-;i>=;i--){
for(int j=;j<=man_in_row;j++){
if(j!=) printf(" ");
cout<<matrix[i][j];
}
printf("\n");
} return ;
}
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
1109 Group Photo的更多相关文章
- 1109 Group Photo (25 分)
1109 Group Photo (25 分) Formation is very important when taking a group photo. Given the rules of fo ...
- PAT 1109 Group Photo[仿真][难]
1109 Group Photo(25 分) Formation is very important when taking a group photo. Given the rules of for ...
- 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
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
- 1109 Group Photo (25分)
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
- PAT (Advanced Level) 1109. Group Photo (25)
简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT甲题题解-1109. Group Photo (25)-(模拟拍照排队)
题意:n个人,要拍成k行排队,每行 n/k人,多余的都在最后一排. 从第一排到最后一排个子是逐渐增高的,即后一排最低的个子要>=前一排的所有人 每排排列规则如下: 1.中间m/2+1为该排最高: ...
- 【PAT甲级】1109 Group Photo (25分)(模拟)
题意: 输入两个整数N和K(N<=1e4,K<=10),分别表示人数和行数,接着输入N行每行包括学生的姓名(八位无空格字母且唯一)和身高([30,300]的整数).按照身高逆序,姓名字典序 ...
- A1109. Group Photo
Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...
随机推荐
- 安装git工具在ubuntu系统
Git is one of the most popular tools used for distributed version control system(VCS). Git is common ...
- EF利用重写SaveChanges()方法实现 审计日志记录
先上一段最近项目中的代码,此代码可以放到自己项目中的dbContext中 public override Task<int> SaveChangesAsync() { List<Au ...
- SSI服务端包含技术
1.页面拆出来怎么样通过web服务浏览呢? 使用web服务(例如nginx)的SSI技术,将多个子页面合并渲染输出. 2.SSI是什么? 3. ssi包含类似于jsp页面中的incluce指令,ssi ...
- hdu 1010(DFS) 骨头的诱惑
http://acm.hdu.edu.cn/showproblem.php?pid=1010 题目大意从S出发,问能否在时间t的时候到达终点D,X为障碍 需要注意的是要恰好在t时刻到达,而不是在t时间 ...
- dpdk优化相关 转
注:本文是参照了一些其他文章,原文地址点击这里. 首先根据这篇文章进行了性能瓶颈的分析 策略与方法 首先根据木桶原理,首先要找到最弱的地方,怎么找往上看↑. 想能优化需要考虑如下: 优化BIOS设置 ...
- 立即响应ScrollView上的子视图的手势
self.myScrollView.delaysContentTouches = YES; self.myScrollView.CanCancelContentTouches=NO; 写了一个继承sc ...
- POJ 2449Remmarguts' Date 第K短路
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 29625 Accepted: 8034 ...
- JeeSite 4.0
http://jeesite.com/ JeeSite 是一个 Java EE 企业级快速开发平台,基于经典技术组合(Spring Boot.Spring MVC.Apache Shiro.MyBat ...
- Laravel policy 的应用
Laravel 提供更简单的方式来处理用户授权动作.类似用户认证,有 2 种主要方式来实现用户授权:gates 和策略,我这里主要讲解下策略的使用. 文档 上面有详细的说明,我这里只根据自己使用过程做 ...
- Serial-mcu
任务: PC按下1键, mcu连续发送a, 当PC按下2键, 终止发送 查询: #include <reg52.h> #define uint unsigned int #define u ...