THTOG - Editorial

PROBLEM LINK:

Practice
Contest Link

Author: Abhinav Prakash
Tester: Abhinav Prakash

DIFFICULTY:

MEDIUM

PREREQUISITES:

Sorting, Math.

PROBLEM:

problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Chef’s game in the order of the numbers written on the iPad. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.

SOLUTIONS:

Setter's Solution
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int count3(LL x) {
	int ret = 0;
	while (x % 3 == 0) {
		ret++;
		x /= 3;
	}
	return ret;
}
int n;
vector<pair<int, LL>> v;
int main() {
	cin >> n;
	v.resize(n);
	for (int i = 0; i < n; i++) {
		cin >> v[i].second;
		v[i].first = -count3(v[i].second);
	}
	sort(v.begin(), v.end());
	for (int i = 0; i < n; i++)
		printf("%lld%c", v[i].second, " \n"[i + 1 == n]);
}