题目描述
照毕业照一定要排好队,不然就会有人被挡住,造成终身遗憾。假设有N个毕业生,准备排K行,拍毕业照的摄影师定下这么几条规矩: 1、每一行的人数一定是N/K个,如果有多的同学,全部站最后一排; 2、后排的一定不能比前排任何一个同学矮; 3、在任意一排,个子最高的站中间,之后次高的站他的右边,第三高的站他的左边,第四高的又站右边,这样依次轮换。例如,有5个同学,身高依次是190厘米,188厘米,186厘米,175厘米和170厘米,那么最后应该排成175,188, 190,186, 170。这里我们假设摄影师面向同学们站立,这样摄影师的左手就是同学们的右手。 4、如果身高相同,那就按姓名的字母序的升序排列,我们假定没有重名的。
输入格式:
每个输入包括一个测试用例,第一行有两个正整数N和K,其中N是毕业生总数,K是排数。 N<=10000,K<=10,随后N行,分别给出不超过8个字符的姓名以及他们的身高,姓名中不含空格。
输出格式:
对每一组输入,显示出毕业生拍照时的位置情况,要求打印K行学生的姓名,姓名之间以1个空格分隔,行尾不得有多余空格。因为你是面向毕业生的,所以在最后一排的同学应该显示在第一行,在第一排的同学显示在最后一行。
输入样例:
在这里给出一组输入。例如:
10 3
Tom 188
Mike 170
Eva 168
Tim 160
Joe 190
Ann 168
Bob 175
Nick 186
Amy 160
John 159
输出样例:
在这里给出相应的输出。例如:
Bob Tom Joe Nick
Ann Mike Eva
Tim Amy John
参考代码
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
#define MAX_N 10005
struct node
{
char name[100];
int heigh;
int h;
int num;
}date[MAX_N];
int N,K;
bool cmp1(const node a,const node b)
{
if(a.heigh==b.heigh)
return strcmp(a.name,b.name) < 0;
else
return a.heigh > b.heigh;
}
bool cmp2(const node a,const node b)
{
if(a.h==b.h)
return a.num < b.num;
else
return a.h < b.h;
}
void f()
{
int t = 0;
int flag = 1;
int first = (N%K)?(N/K+N%K):N/K;
int i,j;
int node_h = 1;
for(i = 1;i<=N;)
{
if(i==1)
{
for(j = 1;j<=first;j++)
{
date[i].h = node_h;
date[i].num = t;
flag = (-1)*flag;
i++;
t *= -1;
t += flag;
}
}
else
{
for(j = 1;j<=N/K;j++)
{
date[i].h = node_h;
date[i].num = t;
flag = (-1)*flag;
i++;
t *= -1;
t += flag;
}
}
t = 0;
flag = 1;
node_h ++;
}
}
void print_ans()
{
int first = (N%K)?(N/K+N%K):N/K;
int i,j;
int count_ = 0;
int kk = N/K;
for(i = 1;i<=first;i++)
{
cout << date[i].name;
if(i!=first)
cout << " ";
}
cout << endl;
for(i;i<=N;i++)
{
cout << date[i].name;
count_ ++;
if(count_%kk==0&&i!=N)
cout << endl;
else if(count_%kk)
cout << " ";
}
}
void print()
{
cout <<"/***************"<<endl;
for(int i = 1;i<=N;i++)
{
cout << date[i].heigh << " ";
}
cout << endl;
for(int i = 1;i<=N;i++)
{
cout << date[i].name << " ";
}
cout << endl;
for(int i = 1;i<=N;i++)
{
cout << date[i].num << " ";
}
cout << "\n**************/"<<endl;
cout << endl;
}
int main()
{
//freopen("test.in","r",stdin);
//freopen("test.out","w",stdout);
int i,j;
cin >> N >> K;
for(i = 1;i<=N;i++)
{
cin >> date[i].name >> date[i].heigh;
}
//比较身高
sort(date+1,date+1+N,cmp1);
//print();
//排列
f();
//print();
//全规则排序
sort(date+1,date+1+N,cmp2);
//print();
//输出答案
print_ans();
return 0;
}