1109 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 (≤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

题目大意:模拟拍合照,假设有N个人需要排K行,有几点要求:

1.每行必须有N/K个人(四舍五入),剩下的人排到最后一行

2.在后一排的所有人不能比首排的人矮

3.每一排中,最高的人站C位(m/2+1),

4.每一排中的站法,先往最高的人右手边站,再往左手边站

5.当身高一样时,按名字字母升序排列。

//猛一看就觉得很难,很多要求,这是个仿真类的题目吧。

//每一行内的人用什么数据结构来存呢?可以左右插入!

#include <iostream>
#include <cstring>
#include<stdio.h>
using namespace std;
struct PE{
int height;
char name[];
}pe[],pe2[];
int n,k;
bool cmp(PE& a,PE &b){
if(a.height>b.height)
return true;
else if(a.height==b.height&&strcmp(a.name,b.name)>)
return true;
return false;
}
int main() {
scanf("%d %d",&n,&k);
char na[];
int h;
for(int i=;i<n;i++){
scanf("%s %d",pe[i].name,&pe[i].height);//这里无效赋值。。
}
sort(pe,pe+n,cmp);
int m=n/k;//一共k排,出最后行外每排m个人
for(int i=;i<k;i++){
int st=i*k;
for(int j=st;j<st+m;j++){
//这right和left乱放太难控制了吧。
//感觉实现就在眼前,就是写不出来。。气死了。
}
} return ;
}

//这是我写的,写到一半写不下去了,因为不会安排。

代码来自:https://www.liuchuo.net/archives/1926

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct node {
string name;
int height;
};
int cmp(node a, node b) {
return a.height != b.height ? a.height > b.height : a.name < b.name;
//这个一句代码,确实比我那个有好几return的好!
}
int main() {
int n, k, m, i, j;
cin >> n >> k;
vector<node> stu(n);
for(i = ; i < n; i++)
cin >> stu[i].name >> stu[i].height;
sort(stu.begin(), stu.end(), cmp);
int t = , row = k;
while(row) {
if(row == k)
m = n - n / k * (k - );//这是在安排最后一行的人数。
else
m = n / k;
vector<string> stemp(m);//每一行都重新定义一个向量。
stemp[m / ] = stu[t].name;//这个中间值很厉害了,
//题目中给的是m/2+1,但是题目中是从1开始计数,数组中下标从0开始。
// 左边一列
j = m / - ;
for(i = t + ; i < t + m; i = i + )
stemp[j--] = stu[i].name;
// 右边一列
j = m / + ;
for(i = t + ; i < t + m; i = i + )
stemp[j++] = stu[i].name;
// 输出当前排
cout << stemp[];
for(i = ; i < m; i++) cout << " " << stemp[i];
cout << endl;
t = t + m;
row--;
}
return ;
}

//厉害了,学习了。

1.其中cmp函数就很值得学习,比我之前那样写好看多了;

2.因为最终输出是从高到低,那么就先安排后排,安排完之后就输出,那么身高就从大到小排列。

3.重点的问题是如何将他们按照身高C位排列。

首先是,找到C位位置,并且将左边和右边分别用一个for来安排,

j呢,是指示C位下标的左右的两个作为开始。

在安排左边的时候是j--,那么对原始排序完毕的就是i+=2,跳一个赋值一个;

对于安排右边的来说,j++,也是i+=2.

2018-9-7更:

那么对于原来的初始排列也有一个i指向它,起点是t,也就是上一次循环设置的,那么整个的可分配的长度就是t~t+m这么长,去按规则左右分配。

//学习了!

PAT 1109 Group Photo[仿真][难]的更多相关文章

  1. PAT 1109 Group Photo

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  2. 1109 Group Photo (25 分)

    1109 Group Photo (25 分) Formation is very important when taking a group photo. Given the rules of fo ...

  3. PAT A1109 Group Photo (25 分)——排序

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  4. 1109 Group Photo

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  5. 1109. Group Photo (25)

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  6. 1109 Group Photo (25分)

    Formation is very important when taking a group photo. Given the rules of forming K rows with N peop ...

  7. PAT (Advanced Level) 1109. Group Photo (25)

    简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  8. PAT甲题题解-1109. Group Photo (25)-(模拟拍照排队)

    题意:n个人,要拍成k行排队,每行 n/k人,多余的都在最后一排. 从第一排到最后一排个子是逐渐增高的,即后一排最低的个子要>=前一排的所有人 每排排列规则如下: 1.中间m/2+1为该排最高: ...

  9. 【PAT甲级】1109 Group Photo (25分)(模拟)

    题意: 输入两个整数N和K(N<=1e4,K<=10),分别表示人数和行数,接着输入N行每行包括学生的姓名(八位无空格字母且唯一)和身高([30,300]的整数).按照身高逆序,姓名字典序 ...

随机推荐

  1. linux中nmcli命令详解

    https://www.iyunv.com/thread-269695-1-1.html http://www.178linux.com/44668

  2. python--数据类型--1

    原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/ 视频教程::http://www.imooc.com/learn/177 http://www. ...

  3. 从头认识java-16.5 nio的数据转换

    这一章节我们来讨论一些nio的数据转换. 上面一章节我们提到ByteBuffer,可是他是面向二进制数据,对于编程来说不是非常方便,因此,java添加了转换数据的工具. 1.asCharBuffer ...

  4. POJ 2923 Relocation(01背包变形, 状态压缩DP)

    Q: 如何判断几件物品能否被 2 辆车一次拉走? A: DP 问题. 先 dp 求解第一辆车能够装下的最大的重量, 然后计算剩下的重量之和是否小于第二辆车的 capacity, 若小于, 这 OK. ...

  5. lua中类的实现原理和实践

    一.基础概念  Lua 本身是函数式的语言,但借助 metatable (元表)这个强大的工具,Lua 实现操作符重载易如反掌.. 下文将详细的解释在Lua中实现类的原理,涉及到的细节点将拆分出来讲, ...

  6. canvas二:绘制圆和其他曲线

    1.绘制圆 绘制圆是canvas里面不可缺少的功课,而且绘制圆在canvas中的用处很多,好嘞,开扯 绘制圆需要用到arc这个方法: arc(X坐标,Y坐标,半径,起始弧度,结束弧度,旋转方向): 弧 ...

  7. 在lampp的proftpd下新增FTP用户的方法与配置

    用LAMPP的安装方法可以开一个默认的lampp用户,不过多用户怎样管理.目录怎样设置?这里简明说一下. 要求:使用Lampp的proftpd,开通多个FTP用户,并各分配一个目录,而且需要限制用户在 ...

  8. HTTP/2笔记之帧

    零.前言 客户端和服务器端一旦握手协商成功接建立连接,端点之间可以基于HTTP/2协议传递交换帧数据了. 一.帧通用格式 下图为HTTP/2帧通用格式:帧头+负载的比特位通用结构: +-------- ...

  9. salt-ssh的批量脚本及使用方法

    author: headsen   chen date : 2018-08-02   20:06:06 1,salt-ssh的安装: yum -y install epel-release yum - ...

  10. android 软键盘回车键捕获

    EditText editText2 = (EditText)findViewById(R.id.txtTest2); editText2.setOnEditorActionListener(new ...