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排(向下取整),最后一排多n % k个人。对于每一排来说,最高的站在每排人数 / 2 + 1的位置,如果按照我们面对队列来说,左大右小,按此规则排序。

#include<iostream>

#include<vector>

#include<algorithm>
#include<cmath>
using namespace std;


struct node
{
    string name;
    int hight;
};

bool cmp(node a, node b){

    return a.hight != b.hight ? a.hight < b.hight : a.name > b.name;
}


int main(){

    int n, k;
    cin >> n >> k;
    int row_num_people = n / k; //每排多少人
    int extra_num = n % k; //最后一排多了多少人
    
    vector<node> vec(n);
    vector<string> vec_out[20];
    for(int i = 0; i < n; i++){
        cin >> vec[i].name;
        scanf(" %d", &vec[i].hight);


    }
    sort(vec.begin(), vec.end(), cmp);
    int peo_cnt;
    for(int i = 1; i < k; i++){
        peo_cnt = i * row_num_people - 1;
        vector<string> temp(row_num_people + 1);
        int flag = row_num_people / 2 + 1;
        bool change = true;
        int pre_l = flag;
        int pre_r = flag;
        while (flag >= 1 && flag <= row_num_people)
        {
            temp[flag] = vec[peo_cnt].name;
            // cout << flag << ":" << vec[peo_cnt].name << endl;
            if(change){ //左
                change = false;
                flag = pre_l - 1;
                pre_l = flag;

            }else //右
            {
                change = true;
                flag = pre_r + 1;
                pre_r = flag;

            }
            
            peo_cnt--;
        }
        vec_out[i] = temp;
    }
    peo_cnt = n - 1;
    vector<string> temp_last(row_num_people + extra_num + 1);
    int flag_last = (row_num_people + extra_num) / 2 + 1;
    bool change_last = true;
    int pre_l_last = flag_last;
    int pre_r_last = flag_last;
    while (flag_last >= 1 && flag_last <= row_num_people + extra_num)
        {
            temp_last[flag_last] = vec[peo_cnt].name;
            if(change_last){ //左
                change_last = false;
                flag_last = pre_l_last - 1;
                pre_l_last = flag_last;

            }else //右
            {
                change_last = true;
                flag_last = pre_r_last + 1;
                pre_r_last = flag_last;

            }
            
            peo_cnt--;
        }

    vec_out[k] = temp_last;


    for(int i = k; i >= 1; i--){


            
        for(int j = 1; j < vec_out[i].size(); j++){

            
            
            printf("%s%s", j == 1 ? "" : " ", vec_out[i][j].c_str());
            
        }
        printf("\n");

    }

    return 0;

}
发布了86 篇原创文章 · 获赞 15 · 访问量 1120

猜你喜欢

转载自blog.csdn.net/zbchenchanghao/article/details/104151763