A1109 Group Photo [模拟]

在这里插入图片描述
照相排队真站位问题 高的站中间 矮的站两边
思路:排序后中间插高的,以这个为结点,左右插入最高的

#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
struct student
{
	string name;
	int height;
}stu[10001];
string a[100001];
bool cmp(student a, student b)
{
	if (a.height != b.height)
		return a.height > b.height;
	else
		return a.name < b.name;
}
int main() {
	int n, m;
	cin >> n >> m;
	int people = n / m;
	for (int i = 0; i < n; i++)
	{
		cin >> stu[i].name >> stu[i].height;
	}
	int num = 0;
	sort(stu, stu + n, cmp);
	for (int i = 0; i < m; i++)
	{
		int temp = people;
		if(i==0)
			temp = people + n % m;
		int tallestindex = temp / 2;
		a[tallestindex] = stu[num++].name;
		int len = 1;
		for (int i = 1; i < temp; i++)
		{
			if (i % 2 != 0)
			{
				a[tallestindex - len] = stu[num++].name;
			}
			else
			{
				a[tallestindex + len++] = stu[num++].name;
			}
		}
		for (int i = 0; i < temp-1; i++)
		{
			cout << a[i] << " ";
		}
		cout << a[temp - 1] << endl;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/Hsiung123/p/13811979.html