1109 Group Photo(25 分)

#include
#include
#include
using namespace std;
const int maxn = 1e4 + 10;
struct peo {
    string name;
    int h;
    bool operator<(const peo&x)const
    {
        return h == x.h ? name < x.name: h > x.h;
    }
}a[maxn];
int n, k, m;
void show(int st, int ed)
{
    string row[1000];
    int left = 1, right = 1;
    for (int i = st; i < ed; i++)
    {
        if (i == st)row[m / 2 + 1] = a[i].name;
        else if ((i-st) % 2 == 1)
        {
            row[m / 2 + 1 - left] = a[i].name;
            left++;
        }
        else if ((i - st) % 2 == 0)
        {
            row[m / 2 + 1 + right] = a[i].name;
            right++;
        }
    }
    int i = 0;
    while (row[i] == "")i++;
    for (int j = i; j < i + ed - st; j++)
    {
        cout << row[j];
        if (j != i+ed - st - 1)cout << " ";
    }
    printf("\n");
}
int main()
{
    scanf("%d%d", &n, &k);
    m = n / k;
    for (int i = 0; i < n; i++)cin >> a[i].name >> a[i].h;
    sort(a, a + n);
    
    int lastrow = n%k + n / k;
    for (int i = 0; i < k; i++)
    {
        if (i == 0)
        {
            show(0, lastrow);
        }
        else
        {
            show(lastrow + (i - 1)*m, lastrow + i*m);
        }
    }
    return 0;
}

你可能感兴趣的:(1109 Group Photo(25 分))