#include<algorithm>
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include <deque>
using namespace std;
typedef struct Node
{
char name[10];//名字
int height;//身高
}Node;
const int MAX = 10010;
Node P[MAX];
bool cmp(Node A, Node B)//个子高的在前,高低相同按名字字典序排序
{
if(A.height != B.height) return A.height > B.height;
return strcmp(A.name, B.name) < 0;
}
int main()
{
int N, K;
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, cmp);//排序
int cnt = 0, num = N/K;
while(cnt < N)
{
deque<char*> Q;//双端队列
int temp = num;//temp为每行的人数
if(!cnt) temp += N%K;
for(int i=0; i<temp && cnt<N; i++)//交替从两端插入
{
if(i%2) Q.push_front(P[cnt++].name);
else Q.push_back(P[cnt++].name);
}
printf("%s", Q.front());//打印该行
Q.pop_front();
while(!Q.empty())
{
printf(" %s", Q.front());
Q.pop_front();
}
printf("\n");
}
return 0;
}
PAT (Advanced Level) Practice A1109 Group Photo (25 分)(C++)(甲级)(双端队列,deque)
最新推荐文章于 2020-11-26 21:32:20 发布