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 ...
随机推荐
- python 面向对象编程 之 上下文管理协议
with open('path', 'r' ,encoding='utf-8') as f: 代码块 上述就叫做上线文管理协议,即with语句,为了让一个对象兼容with语句,必须在这个对象的类中声明 ...
- glide install失败 Update failed for golang.org/x/net: Cannot detect VCS
失败信息: [WARN] Unable to checkout golang.org/x/net[ERROR] Update failed for golang.org/x/net: Ca ...
- PAT 1002 写出这个数 (20)(代码)
1002 写出这个数 (20)(20 分) 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10^100 ...
- ObjC.primitive-methods
Primitive Method "When it comes to subclassing, knowing which methods are ‘primitive’ methods i ...
- mac/linux 修改vim显示信息
转自:http://www.cnblogs.com/yjmyzz/p/4019783.html 步骤1: cp /usr/share/vim/vimrc ~/.vimrc 先复制一份vim配置模板到个 ...
- BZOJ1084或洛谷2331 [SCOI2005]最大子矩阵
BZOJ原题链接 洛谷原题链接 注意该题的子矩阵可以是空矩阵,即可以不选,答案的下界为\(0\). 设\(f[i][j][k]\)表示前\(i\)行选择了\(j\)个子矩阵,选择的方式为\(k\)时的 ...
- CH6802 車的放置
原题链接 和棋盘覆盖(题解)差不多. 将行和列看成\(n+m\)个节点,且分属两个集合,如果某个节点没有被禁止,则行坐标对应节点向列坐标对应节点连边,然后就是求二分图最大匹配了. #include&l ...
- Why Linux Doesn’t Need Defragmenting
If you’re a Linux user, you’ve probably heard that you don’t need to defragment your Linux file syst ...
- [SoapUI] Property Expansion in soapUI
1. Property Expansion in soapUI SoapUI provides a common syntax to dynamically insert ("expand& ...
- 我的MVP呢?
Ladies and gentelmen, welcome the MVP of NBA 16-2017 Season:... 呃,等下,好像哪里不对.那是因为,我要说的MVP根本就不是Most Va ...