1055 集体照

Text Summary

To solve this problem, condier these four essential points:

  1. Storing people using a ‘Person’ struct and a two-dimensional vector:
struct Person {
	string name;
	int height;
};

vector<vector<Person> > ans(k);
  1. Sorting the ‘Person’ struct based on height and name:
bool cmp(const Person& a, const Person& b) {
	if (a.height == b.height)
		return a.name < b.name;
	else
		return a.height > b.height;
}
sort(v.begin(), v.end(), cmp);
  1. Calculating the number of people in each row:
int column = n / k;
// The last row = column + n % k
  1. Populating each row with people in a zigzag pattern:
for (i = 0; i < column + n % k and t < n; i++) {
	if (i % 2 == 0) ans[0].push_back(v[t++]);
	else ans[0].insert(ans[0].begin(), v[t++]);
}
for (i = 1; i < k and t < n; i++) {
	for (int j = 0; j < column and t < n; j++){
		if (j % 2 == 0) ans[i].push_back(v[t++]);
		else ans[i].insert(ans[i].begin(), v[t++]);
	}
}

Code Summary

#include <bits/stdc++.h>

using namespace std;

struct Person {
	string name;
	int height;
};

bool cmp(const Person& a, const Person& b) {
	if (a.height == b.height)
		return a.name < b.name;
	else
		return a.height > b.height;
}

int main() {
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif

	int n, k;
	cin >> n >> k;
	int column = n / k;
	vector<Person> v(n);
	vector<vector<Person> > ans(k);
	int i;
	for (i = 0; i < n; i++)
		cin >> v[i].name >> v[i].height;
	sort(v.begin(), v.end(), cmp);
	int t = 0;
	for (i = 0; i < column + n % k and t < n; i++) {
		if (i % 2 == 0) ans[0].push_back(v[t++]);
		else ans[0].insert(ans[0].begin(), v[t++]);
	}
	for (i = 1; i < k and t < n; i++) {
		for (int j = 0; j < column and t < n; j++){
			if (j % 2 == 0) ans[i].push_back(v[t++]);
			else ans[i].insert(ans[i].begin(), v[t++]);
		}
	}

	for (i = 0; i < k; i++) {
		for (int j = 0; j < ans[i].size(); j++) {
			if (j) cout << ' ';
			cout << ans[i][j].name;
		}
		cout << endl;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值