专栏首页ACM算法日常除以3,乘以2(STL+排序)- Codeforces 997D

除以3,乘以2(STL+排序)- Codeforces 997D

作者:AlphaWA

(友情提示:不喜欢看英文的童鞋,可以先翻到下面看翻译

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⋅10^18) — 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][9,3,6,12,4,8]. It can match possible Polycarp's game which started with x=9.

概译:

对一个数进行连续变换,要么除以3要么乘2,比如这个数是126,除以3是42,乘2是84,除以3是28。这个除以3必须是3的整倍数,且除以3和乘2选择哪个都可以没有顺序要求。这样这个序列A为:126,42,84,28。输入文件会给出一个乱序的B比如:42,28,84,126,然后我们来找出原来的序列A并输出。

思路:

这是一个div3的题目,div3是新开的面向入门选手的比赛模式,大家可以尝试一下。AlphaWA写这个题时用了类似搜索的方式,先随便找个作为标杆的数(比如我直接用了a[1]),然后所求序列中在它之前的数储存在一个栈中,在它之后的储存在一个队列中,最后先后输出栈和队列中的数即可。

(以前太懒了这次帮忙画个图?(`・ω・´))

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;//3*10^18已经爆int,需要longlong

map<ll, bool>m; //map记录是否存在某个数
stack<ll>s;
queue<ll>q;

int main()
{
    int n;
    ll a[105];
    //输入
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) cin >> a[i], m[a[i]] = true;
    //用栈储存在a[1]前面的
    s.push(a[1]);
    ll k = s.top();
    do
    {
        if (m[k * 3]) s.push(k * 3), k *= 3;
        else if (k % 2 == 0 && m[k / 2]) s.push(k / 2), k /= 2;
        else break;
    } while (true);
    //用队列储存在a[1]后面的
    k = a[1];
    do
    {
        if (k % 3 == 0 && m[k / 3]) q.push(k / 3), k /= 3;
        else if (m[k * 2]) q.push(k * 2), k *= 2;
        else break;
    } while (true);
    //输出
    while (s.size())
    {
        cout << s.top() << ' ';
        s.pop();
    }
    while (q.size())
    {
        cout << q.front() << ' ';
        q.pop();
    }

    return 0;
}

而官方题解就比较简单了,用了pair的排序特性,以前我们写过的一道cf上的div2的题也是巧用了pair的排序特性,即:先排first后排second。其实不用pair自己自定义sort的比较函数也ok只是正好两个元素没这个方便。

这些数可以分为一些种类:能除以3且能连除n次的,连除n-1次的,……1次的,0次的。举样例:

#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);//设置vector大小为n

    for (int i = 0; i < n; i++) {
        cin >> v[i].second;
        v[i].first = -count3(v[i].second); //这里要count3更大的数排前面,所以取负
    }

    sort(v.begin(), v.end());

    for (int i = 0; i < n; i++)
        printf("%lld%c", v[i].second, " \n"[i + 1 == n]);//学下这波语法

    return 0;
}

可以无视的后话:经常有同学问C和C++的事情,这道题就疯狂使用了STL,想参加算法竞赛的同学要抓紧学一下STL了,很方便哒!

(都看到这里了,给我们的小伙伴一个赞呗!

本文分享自微信公众号 - ACM算法日常(acm-clan),作者:dansen

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2018-06-11

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 独角兽与数列(置换群循环)- HDU 4985

    群论是法国数学家伽罗瓦(Galois)的发明。伽罗瓦是一个极具传奇性的人物,年仅21岁就英年早逝于一场近乎自杀的决斗中。他用该理论,具体来说是伽罗瓦群,解决了五...

    ACM算法日常
  • 搜索专题4 | 旋转棋盘 POJ - 2286

    The rotation game uses a # shaped board, which can hold 24 pieces of square bloc...

    ACM算法日常
  • POJ2318 TOYS 判断点与直线位置关系 【计算几何】

    Calculate the number of toys that land in each bin of a partitioned toy box.

    ACM算法日常
  • Web前端学习(打卡机)

    在补齐了Java后端的短板之后,我准备补补前端的课。这篇文章可以作为我前端学习的过程记录(打开机)。

    阿杜
  • POJ-1953 World Cup Noise(线性动规)

    World Cup Noise Time Limit: 1000MS Memory Limit: 30000K Total Submissio...

    ShenduCC
  • Codeforces 1291 Round #616 (Div. 2) C. Mind Control(超级详细)

    You and your n−1 friends have found an array of integers a1,a2,…,an. You have de...

    风骨散人Chiam
  • 不知道IDEA的这些个性化配置,你怎么能说自己会用它

    这里默认提供了三套主题:IntelliJ,Darcula,Windows。这里可以根据自己的喜 好进行选择。

    不温卜火
  • ZOJ 3623 Battle Ships

    Battle Ships Time Limit: 2 Seconds Memory Limit: 65536 KB Battle Ships is a ne...

    风骨散人Chiam
  • CodeForces - 140A New Year Table (几何题)当时没想出来-----补题

    A. New Year Table time limit per test2 seconds memory limit per test256 megaby...

    风骨散人Chiam
  • POJ1611 (并查集)

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetio...

    dejavu1zz

扫码关注云+社区

领取腾讯云代金券