Codeforces 977D. Divide by three, multiply by two
Divide by three, multiply by two
Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n−1 operations of the two kinds:
divide the number x by 3 (x must be divisible by 3);
multiply the number x by 2.
After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.
You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.
Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp’s game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.
It is guaranteed that the answer exists.
Input
The first line of the input contatins an integer number n (2≤n≤100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a1,a2,…,an (1≤ai≤3⋅1018) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.
It is guaranteed that the answer exists.
Examples
input
6
4 8 6 3 12 9
output
9 3 6 12 4 8
input
4
42 28 84 126
output
126 42 84 28
input
2
1000000000000000000 3000000000000000000
output
3000000000000000000 1000000000000000000
Note
In the first example the given sequence can be rearranged in the following way: [9,3,6,12,4,8]. It can match possible Polycarp’s game which started with x=9.
題意:
給你一組數,這組數是由一個數經過除以三,乘以二變化來的,這個數也給了
讓你輸出變化的順序
如給的是4 8 6 3 12 9
那麼答案就是 9 3 6 12 4 8
結果一定是唯一的
這道題,腦袋不知道怎麼了,周賽的時候做沒有做出來,。,,
回頭補了一下,發現不是很難
由於n比較小,
第一種方式我採用的是暴力建連結串列
對於任意兩個數a b
如果 a * 3 == b || a = b * 2
那麼就說明,在生成的序列中 a 在 b 的前面
//暴力建立連結串列
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[128];
int l[128];
bool f[128];
int main()
{
int n;
cin>>n;
memset(f,0,sizeof(f));
memset(l,-1,sizeof(l));
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(i==j) continue;
if(a[i]*2==a[j]||a[i]==a[j]*3) l[i]=j,f[j]=1;// a[i] 在前,a[j]在後 沒有被用過的是頭節點
}
int t = -1;
for(int i=0;i<n;i++)
if(!f[i])
{
t = i;
break;
}
for(t;~t;t=l[t])
printf("%I64d%c",a[t]," \n"[t==-1]);
return 0;
}
第二種方法是:
我們經過觀察到結果序列規律:
能整除3的次數越多,優先順序越高,次數相同的按照從小到大排列
於是我用pair<int,long long> 來存數,第一個存優先順序,第二個存數
對pair進行sort的時候,會自動按照first從小到大排序,first相等時按照second從小到大排序
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 107;
ll a[MAXN];
ll f(ll x)
{
ll ans = 0;
while(x%3==0)
{
x/=3;
ans++;
}
return ans;
}
int main()
{
int n;
cin>>n;
vector<pair<int,ll> > v;
v.resize(n);
for(int i=0;i<n;i++)
{
scanf("%I64d",&v[i].second);
v[i].first = -f(v[i].second);
}
sort(v.begin(),v.end());//pair<> 陣列的性質,先按first從小到大排序,first相等時按照second從小到大排序
for(int i=0;i<n;++i)
printf("%I64d%c",v[i].second," \n"[i==n-1]);
return 0;
}