前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >除以3,乘以2(STL+排序)- Codeforces 997D

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

作者头像
ACM算法日常
发布2018-08-07 17:04:22
5430
发布2018-08-07 17:04:22
举报
文章被收录于专栏:ACM算法日常ACM算法日常

作者: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

代码语言:javascript
复制
6
4 8 6 3 12 9

Output

代码语言:javascript
复制
9 3 6 12 4 8 

Input

代码语言:javascript
复制
4
42 28 84 126

Output

代码语言:javascript
复制
126 42 84 28 

Input

代码语言:javascript
复制
2
1000000000000000000 3000000000000000000

Output

代码语言:javascript
复制
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]),然后所求序列中在它之前的数储存在一个栈中,在它之后的储存在一个队列中,最后先后输出栈和队列中的数即可。

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

代码:

代码语言:javascript
复制
#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次的。举样例:

代码语言:javascript
复制
#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了,很方便哒!

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

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-06-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 ACM算法日常 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档