1109. Group Photo 解析

  • Post author:
  • Post category:其他


感觉是个排序应用。挺像HASH里面的解决冲突的。

整个身高排序,然后按要求排就好啦。

#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <vector>

#define MAX 12

using namespace std;

struct people {
	string name;
	int high;
};
vector <people> list;
int n,k;

bool cmp(people p1, people p2) {
	if (p1.high > p2.high)
		return true;
	else if (p1.high == p2.high && p1.name < p2.name)
		return true;
	return false;
}

int main() {
	cin >> n >> k;

	int m = n / k;
	int left = n % k;
	int row = n / m;

	people formation[10005];

	people temp;
	for (int i = 0; i < n; i++) {
		cin >> temp.name >> temp.high;
		list.push_back(temp);
	}

	sort(list.begin(), list.end(), cmp);

	int pos = 0;

	for (int i = 0; i < row; i++) {
		int tempM = m;
		if (i == 0) 
			tempM = m + left;
		int mid = tempM / 2 + 1;
		int len = 1;
		formation[mid] = list[pos++];

		for (int i = 1; i < tempM; i++) {
			if (i % 2 != 0) {
				formation[mid - len] = list[pos++];
			}
			else
				formation[mid + len++] = list[pos++];
		}

		for (int i = 1; i < tempM; i++) {
			cout << formation[i].name << " ";
		}
		cout << formation[tempM].name << endl;
	}

	return 0;
	
}



版权声明:本文为sheepyWYY原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。