Basic Level 1055 group photo (25 points)

topic

Formation is very important when taking group photos. Here, the queuing rules for a given N-person K-line formation are as follows:

The number of people in each row is N/K (rounded down), and all the extra people stand in the last row;

No one in the back row is shorter than anyone in the front row;

The highest in each row stands in the middle (the middle position is m/2+1, where m is the number of people in the row, and the division is rounded down);

The other people in each row take the middleman as the axis, in a non-increasing order of height, and alternate from right to left to stand on both sides of the middleman (for example, if the height of 5 people is 190, 188, 186, 175, 170, the formation is 175, 188, 190, 186, 170. It is assumed here that you are facing the photographer, so your left is the right of the middleman);

If multiple people have the same height, they are sorted in ascending lexicographical order of their names. No duplicate names are guaranteed here.

Given a group of photographers, please write a program to output their formation.

Input format:

Each input contains 1 test case. The first line of each test case gives two positive integers N (≤ 1 0 4, the total number of people) N (≤10^4, the total number of people)N104 ,totalpersoncount)and K (≤10, the total number of rows). Next N lines, each line gives a person's name (no spaces, no more than 8 English letters) and height (an integer in the interval [30, 300]).

Output format:

Output the photographed formation. Namely, the names of row K are separated by spaces, and there must be no extra spaces at the end of the line. Note: Assuming you are facing the photographer, the output of the person in the back row is at the top, and the output of the front row is at the bottom.

Input sample:

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

Code:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10010;
int N, K;
struct stu{
    
    
    string name;
    int high;
}student[MAXN];
bool cmp(stu a, stu b){
    
     return a.high != b.high ? a.high > b.high : a.name < b.name;}
int main(){
    
    
    cin >> N >> K;
    for(int i = 0; i < N; i++) cin >> student[i].name >> student[i].high;
    sort(student, student + N, cmp);
    for(int i = K, m = 0, t = 0; i > 0; i--, t += m){
    
    
        if(i == K) m = N - N / K * (K - 1);
        else m = N / K;
        vector<string> s(m);
        s[m / 2] = student[t].name;
        for(int j = t + 1, k = m / 2 - 1; j < t + m; j += 2) s[k--] = student[j].name;
        for(int j = t + 2, k = m / 2 + 1; j < t + m; j += 2) s[k++] = student[j].name;
        for(int i = 0; i < m; i++) printf("%s%s", i ? " " : "", s[i].c_str());
        cout << endl;
    }
    return 0;
}

PAT_BasicLevel

Guess you like

Origin blog.csdn.net/zy440458/article/details/113807047