PAT(A) 1109. Group Photo (25)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/huqiao1206/article/details/79487187

原题目:

原题链接:https://www.patest.cn/contests/pat-a-practise/1109

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 (<=10000), 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

题目大意

题目要求给一些人排队,前排任意一人比后排的要低,每一排最高的站中间,次高在左,再次高在右,然后左右左右如是。

解题报告

排序是必然的了,其次是计算每行的人数,以及排序方案。
1. 除最后一行外,其余行都是(m = N / K)个人,最后一行减一下就有。
1. 对于每一行,假设按身高由高到低的人的下标是1,2,3……,最左边的必然是最大偶数,然后依次减2,2右边是1,然后依次加2,假设是1-10排的话,结果是 10 8 6 4 2 1 3 5 7 9 。

代码

/*
* Problem: 1109. Group Photo (25)
* Author: HQ
* Time: 2018-02-08
* State: Done
* Memo: 排序 手动模拟
*/
#include "iostream"
#include "string"
#include "algorithm"
#include "vector"
using namespace std;

struct Stu {
    string name;
    int height;
};

int cmp(struct Stu a, struct Stu b) {
    if (a.height == b.height)
        return a.name < b.name;
    return a.height > b.height;
}

vector<struct Stu> students;
int N = 0, K = 0;

void printAns() {
    int m = N / K;
    int start = 0;
    int n;
    bool first;
    for (int row = 1; row <= K; row++) {
        first = true;
        n = N - (K - row) * m - start;
        int i = n >> 1 << 1;
        while (i) {
            if (first) {
                cout << students[start + i - 1].name;
                first = false;
            }else
                cout << " " << students[start + i - 1].name;
            i = i - 2;
        }
        i = 1;
        while (i <= n) {
            if (first) {
                cout << students[start + i - 1].name;
                first = false;
            }
            else
                cout << " " << students[start + i - 1].name;
            i = i + 2;
        }
        start += n;
        cout << endl;
    }
}

int main() {
    cin >> N >> K;
    for (int i = 0; i < N; i++) {
        struct Stu temp;
        cin >> temp.name >> temp.height;
        students.push_back(temp);
    }
    sort(students.begin(), students.end(), cmp);
    printAns();
    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/huqiao1206/article/details/79487187