题目描述
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.
翻译:在合影时,队形很重要。给定N人组成K行的规则如下:
- 每一行的人数必须是N/K(四舍五入到最近的整数),所有多余的人(如果有)都站在最后一行。
- 所有在后排的人不得矮于任何站在前排的人。
- 在每一行,最高的一个人站在中央位置(即定义为位置(m/2+1),其中m是这一行的总人数,除法的结果必须四舍五入到最近的整数)。
在每一行,其他人必须按照各自高度降序排序,交替确定位置,先选择最高的右边,再是最高的左边,(例如五人的高度190,188,186,175,到170年,最后形成的是175 188 190 186 170。这里我们假设您正处在面临该队伍的一个中央位置,你的左手边是右边。)
有许多人拥有相同的高度时,他们必须在名字的字母顺序升序排序,并且数据保证没有重复的名字。
现在给定一组人的信息,您需要编写一个程序来输出他们的信息。
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]).
翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行包括两个正整数,分别为N(≤104),代表总人数;和K (≤10), 代表总行数。接下来N行,每行给出一个人的姓名(不超过8位英文字母,中间没有空格)和他/她的身高。(一个在 [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.
翻译:对于每组输入数据,输出编队——即输出K行人们的姓名。姓名之间必须用一个空格隔开,但是行末尾不能有多余空格。注意:由于你正对队伍,最里面那行的人必须在前面行的人之前输出。
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
解题思路
新建一个People类型结构体并重载<符号,排序输出。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<map>
#include<queue>
#include<algorithm>
#define INF 99999999
using namespace std;
int N,K;
struct People{
char name[20];
int height;
bool operator <(const People a)const{
return a.height == height ? strcmp(name,a.name)<0 : a.height < height;
}
}p[10010];
vector<People> v;
int main(){
scanf("%d %d",&N,&K);
for(int i=0;i<N;i++){
scanf("%s %d",p[i].name,&p[i].height);
}
sort(p,p+N);
int pre=0,pro=0,tmp=N/K,temp=0;
for(int j=0;j<K;j++){
temp=tmp;
if(j==0)temp+=N%K;
pro=pre+temp;
for(int i=pre;i<pro;i++){
if((i-pre)%2==0) v.push_back(p[i]);
else v.insert(v.begin(),p[i]);
}
for(int i=0;i<v.size();i++){
if(i==0)printf("%s",v[i].name);
else printf(" %s",v[i].name);
}
printf("\n");
v.clear();
pre+=temp;
}
return 0;
}