1109 Group Photo (25分)

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排的队形设计排队规则如下:
  • 每排⼈人数为N/K(向下取整),多出来的⼈人全部站在后⼀一排;后排所有⼈人的个⼦子都不不⽐比前排任何⼈人 矮;每排中⾼高者站中间(中间位置为m/2+1,其中m为该排⼈人数,除法向下取整);每排其他⼈人以中 间⼈人为轴,按身⾼高⾮非增序,先右后左交替⼊入队站在中间⼈人的两侧(例例如5⼈人身⾼高为190、188、186、 175、170,则队形为175、188、190、186、170。这⾥里里假设你⾯面对拍照者,所以你的左边是中间⼈人的右 边);若多⼈人身⾼高相同,则按名字的字典序升序排列列。这⾥里里保证⽆无重名。现给定⼀一组拍照⼈人,请编写 程序输出他们的队形。输出拍照的队形。即K排⼈人名,其间以空格分隔,⾏行行末不不得有多余空格。注 意:假设你⾯面对拍照者,后排的⼈人输出在上⽅方,前排输出在下⽅方~
  • 分析:建⽴立结构体node,⾥里里⾯面包含string类型的姓名name和int类型的身⾼高height~将学⽣生的信息输⼊入 到node类型的vector数组stu中~然后对stu数组进⾏行行排序(cmp函数表示排序规则,如果身⾼高不不等,就 按照身⾼高从⼤大到⼩小排列列;如果身⾼高相等,就按照名字从⼩小到⼤大的字典序排列列~)然后⽤用while循环排列列 每⼀一⾏行行,将每⼀一⾏行行应该排列列的结果的姓名保存在ans数组中~
  • 因为是⾯面对拍照者,后排的⼈人输出在上⽅方,前排输出在下⽅方,每排⼈人数为N/K(向下取整),多出来 的⼈人全部站在后⼀一排,所以第⼀一排输出的应该是包含多出来的⼈人,所以while循环体中,当row == k 时,表示当前是在排列列第⼀一⾏行行,那么这⼀一⾏行行的⼈人数m应该等于总⼈人数n减去后⾯面的k列列*(k-1)⾏行行,即m = n – n / k * (k-1);如果不不是第⼀一⾏行行,那么m直接等于n / k;中间⼀一个学⽣生应该排在m/2的下标位置,即 ans[m / 2] = stu[t].name;然后排左边⼀一列列,ans数组的下标 j 从m/2-1开始,⼀一直往左j–,⽽而对于stu的 下标 i,是从t+1开始,每次隔⼀一个⼈人选取(即i = i+2,因为另⼀一些⼈人的名字是给右边的),每次把stu[i] 的name赋值给ans[j–];排右边的队伍同理理,ans数组的下标 j 从m/2 + 1开始,⼀一直往右j++,stu的下标 i,从t+2开始,每次隔⼀一个⼈人选取(i = i+2),每次把stu[i]的name赋值给ans[j++],然后输出当前已经 排好的ans数组~每⼀一次排完⼀一列列row-1,直到row等于0时退出循环表示已经排列列并输出所有的⾏行行~
#include <bits/stdc++.h>
using namespace std;
struct st
{
    string name;
    int height;
};
int cmp(struct st a,struct st b)
{
    return a.height!=b.height?a.height>b.height:a.name<b.name;
}
int main()
{
    int n,k,m;
    cin>>n>>k;
    vector<st>s(n+1);
    for(int i=0; i<n; i++)
    {
        cin>>s[i].name;
        cin>>s[i].height;
    }
    sort(s.begin(),s.end(),cmp);
    int t=0,row=k;
    while(row)
    {
        if(row==k)
        {
            m=n-n/k*(k-1);
        }
        else
        {
            m=n/k;
        }
        vector<string>ans(m);
        ans[m/2]=s[t].name;
        int j=m/2-1;
        for(int i=t+1; i<t+m; i+=2)
            ans[j--]=s[i].name;
        j=m/2+1;
        for(int i=t+2; i<t+m; i=i+2)
            ans[j++]=s[i].name;
        cout<<ans[0];
        for(int i=1; i<m; i++)
            cout<<" "<<ans[i];
        cout<<endl;
        t=t+m;
        row--;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值