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 ...
随机推荐
- supervisor安装、使用详解
supervisor是用python写的一个进程管理工具,用来启动,重启,关闭进程. 1 supervisor的安装 pip install supervisor 2 supervisor的配置文件( ...
- 克隆后没有IP
删除文件: /etc/udev/rules.d/70-persistent-net.rules 将/etc/sysconfig/network-scripts/ifcfg-eth0 中的HWADDR ...
- rbac之 权限粒度控制到按钮级别
rbac之 权限粒度控制到按钮级别: 这里的意思就是 如果当前用户,没有这个权限. 那么这个相对应的这个按钮的权限, 就不应该展示.看都不能给看到. 思路: 为每一个权限,设置一个别名. 这里是这 ...
- css背景图充满屏幕
代码: body { /* 加载背景图 */ background: url(resource/inv_bg.png); /* 背景图不平铺 */background-repeat: no-repea ...
- JavaScript 练习题
练习题 1. 使用for循环输出1到50的值,要求每次循环只能输出一个值,每输出十个换一行. 2 日历生成器: 要求 用户输入,这个月有多少天,本月1号是星期几,自动生成日历 3. 表格生成器 4. ...
- Ajax cookie session form组件
. Cookie是什么 保存在浏览器端的键值对 为什么要有Cookie? 因为HTTP请求是无状态的 Cookie的原理? 服务端可以在返回响应的时候 做手脚 在浏览器上写入键值对(Cookie) 浏 ...
- spring boot (三): 热部署
介绍了Spring boot实现热部署的两种方式,这两种方法分别是使用 Spring Loaded和使用spring-boot-devtools进行热部署. 热部署是什么 大家都知道在项目开发过程中, ...
- BZOJ 1791: [IOI2008]Island 岛屿 - 基环树
传送门 题解 题意 = 找出无向基环树森林的每颗基环树的直径. 我们首先需要找到每颗基环树的环, 但是因为是无向图,用tarjan找环, 加个手工栈, 我也是看了dalao的博客才知道tarjan找无 ...
- 厉害了,他用PS不是P照片而是……
今儿要介绍的主角是战斗民族的设计师 Dmitriy Glazyrin,他这个人用PS做设计有个特点,专门P3D软件做出来的白模. 大家可以想象一下,一个什么颜色什么材质都没有的东西,把它楞是用PS加上 ...
- EL(表达式语言)
EL表达式的主要作用 1)获取数据.EL使得获取JavaBean中的数据变得非常简单,也可以替换JSP页面中的脚本元素,从各种类型的web域中获取数据. 2)执行运算.利用EL表达式可以在JSP页面中 ...