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

本文介绍了一种队列排列算法的实现方法,该算法用于将人员按照特定规则进行排队,确保队列中每个人的身高符合递增条件,并在身高相同的情况下依据姓名字典序进行排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:n个人,要拍成k行排队,每行 n/k人,多余的都在最后一排。

从第一排到最后一排个子是逐渐增高的,即后一排最低的个子要>=前一排的所有人

每排排列规则如下:

1.中间m/2+1为该排最高;

2.其他人按各自降序顺序,轮流排到中间最高的左边和右边;

举个例子 190 188 186 175 170

— — 190 — —

— 188 190 — —

— 188 190 186 —

175 188 190 186 —

175 188 190 186 170

3.当个子一样高时,名字按字典序顺序,靠前的先排入队伍。

纯粹模拟,没啥好说的。

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn=10000+5;


struct People{
    char str[20];
    int height;
    bool operator<(const People tmp)const{
        if(height==tmp.height){
            if(strcmp(str,tmp.str)<0)
                return false;
            else
                return true;
        }
        else{
            return height<tmp.height;
        }
    }
}people[maxn];
int main()
{
    int k,n;
    scanf("%d %d",&n,&k);
    int ans[k+1][maxn];
    int cols[k+1];
    for(int i=0;i<n;i++){
        scanf("%s %d",people[i].str,&people[i].height);
    }
    sort(people,people+n);

    int m=n/k;
    int left,right;
    for(int i=1;i<=k;i++){
        left=(i-1)*m;
        right=i*m-1;
        if(i==k){
            m=n-(k-1)*m;
            right=n-1;
        }
        cols[i]=m;
        int center=m/2+1;
        int idx=right;
        ans[i][center]=idx;
        idx--;
        int l=center-1,r=center+1;
        while(r<=m){
            ans[i][l]=idx;
            idx--;
            ans[i][r]=idx;
            idx--;
            l--;r++;
        }
        if(l==1)
            ans[i][1]=idx;
    }
    for(int i=k;i>=1;i--){
        for(int j=1;j<=cols[i];j++){
            if(j==01){
                printf("%s",people[ans[i][j]].str);
            }
            else{
                printf(" %s",people[ans[i][j]].str);
            }
        }
        printf("\n");
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/chenxiwenruo/p/6131085.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值