Skip to content

Latest commit

 

History

History
77 lines (67 loc) · 1.43 KB

1105.md

File metadata and controls

77 lines (67 loc) · 1.43 KB

1105

image.png

#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool cmp(int a, int b) {
	return a > b;
}

int main() {
	int N;
	cin >> N;
	vector<int> v(N);
	for (int i = 0; i < N; i++) {
		cin >> v[i];
	}
	sort(v.begin(), v.end());
	int m, n;	// m >= n
	for (n = sqrt(N);; n--) {
		if (N % n == 0) {
			m = N / n;
			break;
		}
	}
	// cout << m << " " << n << endl;
	int dx[4] = { 0, 1, 0, -1 };
	int dy[4] = { 1, 0, -1, 0 };
	int x = 0;
	int y = -1;
	vector<vector<int>> ans(m, vector<int>(n, 0));
	while (N) {
		for (int i = 0; i < 4; i++) {
			while (x + dx[i] >= 0 && x + dx[i] < m && y + dy[i] >= 0 && y + dy[i] < n && !ans[x + dx[i]][y + dy[i]]) {
				x += dx[i];
				y += dy[i];
				ans[x][y] = v[--N];
			}
		}
	}
	for (int i = 0; i < m; i++) {
		for (int j = 0; j < n; j++) {
			cout << ans[i][j];
			if (j < n - 1) {
				cout << " ";
			}
		}
		cout << endl;
	}
	return 0;
}

/*
Sample Input:
12
37 76 20 98 76 42 53 95 60 81 58 93
Sample Output:
98 95 93
42 37 81
53 20 76
58 60 76
*/

References