【算法笔记】B1055 集体照

思路:

  对输入的姓名身高按降序排序,然后调整每排的站位并输出,调整站位的时候先确定中心再调整一边比较容易实现。

code

#include<bits/stdc++.h>
using namespace std;
const int maxn = 10010;
struct student{
    string name;
    int height;
};
int cmp(student a, student b) {
    return a.height != b.height ? a.height > b.height : a.name < b.name;
}
int main(){
    int n, k, m;
    cin>>n>>k;
    vector<student> stu(n);
    for(int i = 0; i < n; i++) {
        cin >> stu[i].name >> stu[i].height;
    }
    sort(stu.begin(), stu.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] = stu[t].name;
        int j = m / 2 - 1;
        for(int i = t + 1; i < t + m; i = i + 2)
            ans[j--] = stu[i].name;
        j = m / 2 + 1;
        for(int i = t + 2; i < t + m; i = i + 2)
            ans[j++] = stu[i].name;
        cout << ans[0];
        for(int i = 1; i < m; i++)
            cout << " " << ans[i];
        cout << endl;
        t = t + m;
        row--;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/chunlinn/p/10810819.html