1055 集体照 (25 分)

1055 集体照 (25 分)_第1张图片

#include 
#include
#include
using namespace std;
#define N 10002
typedef struct {
    string name;
    int height;
}node,human[N];
bool compare(node x, node y) {//自定义排序函数
    if (x.height > y.height) {
        return true;
    }
    if (x.height == y.height) {
        if (x.name < y.name) {
            return true;
        }
    }
    return false;
}
int main()
{
    int n, k;
    cin >> n >> k;
    int m = n / k;//每排人数
    human a;
    for (int i = 0;i < n;i++) {
        cin >> a[i].name >> a[i].height;
    }
    sort(a, a + n,compare);
    int last = n - m * (k - 1);//最后一排人数
    int mid = last / 2 + 1;//中间位置
    node put[last+1];
    int left = 1, right = 0;
    bool flag = true;
    //先排好最后一排
    for (int i = 0;i < last;i++) {
        if (flag) {
            put[mid + right] = a[i];
            right++;
            flag = false;
        }
        else {
            put[mid - left] = a[i];
            left++;
            flag = true;
        }
    }
    for (int i = 1;i <= last;i++) {
        if (i != 1)cout << ' ';
        cout << put[i].name;
    }
    cout << endl;
    //再排其他排
    flag = true;
    left = 1, right = 0;
    int jishu = 0;
    int zhong = m / 2 + 1;//每排的中间位置
    for (int i = last;i < n;i++) {
        if (flag) {
            put[zhong + right] = a[i];
            right++;
            jishu++;
            flag = false;
        }
        else {
            put[zhong - left] = a[i];
            left++;
            jishu++;
            flag = true;
        }
        if (jishu == m) {
            flag = true;
            left = 1, right = 0;
            jishu = 0;
            int zhong = m / 2 + 1;//每排的中间位置
            for (int j = 1;j <= m;j++) {
                if (j != 1)cout << ' ';
                cout << put[j].name;
            }
            cout << endl;
        }
    }
    
    return 0;
}

你可能感兴趣的:(1055 集体照 (25 分))