D. Divide by three, multiply by two
                      time limit per test 1 second
                    memory limit per test 256 megabytes
                        input:standard input
                        output:standard output

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=;
while(x % == ){
ret++;
x /= ;
}
return ret;
} int n;
vector<pair<int,LL> > v; int main(){
cin>>n;
v.resize(n);//设置vector大小为n for(int i=; i<n; i++){
cin>>v[i].second;
v[i].first=-count3(v[i].second);//这里要count3更大的数排前面,所以取负
} sort(v.begin(), v.end()); for(int i=; i<n; i++)
printf("%lld%c", v[i].second, " \n"[i + == n]);//学下这波语法 return ;
}

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

Codeforces 997D(STL+排序)的更多相关文章

  1. 详细解说 STL 排序(Sort)

    0 前言: STL,为什么你必须掌握 对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算 ...

  2. 转:详细解说 STL 排序(Sort)

    详细解说 STL 排序(Sort) 详细解说 STL 排序(Sort) 作者Winter 详细解说 STL 排序(Sort) 0 前言: STL,为什么你必须掌握 1 STL提供的Sort 算法 1. ...

  3. 详细解说 STL 排序(Sort)(转)

    作者Winter 详细解说 STL 排序(Sort) 0 前言: STL,为什么你必须掌握 1 STL提供的Sort 算法 1.1 所有sort算法介绍 1.2 sort 中的比较函数 1.3 sor ...

  4. AIM Tech Round 4 (Div. 2)(A,暴力,B,组合数,C,STL+排序)

    A. Diversity time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

  5. STL 排序(转载)

    这篇文章关于STL中的排序写的虽不深入,但是还是挺好的. 1.sort sort有两种形式,第一种形式有两个迭代器参数,构成一个前开后闭的区间,按照元素的 less 关系排序:第二种形式多加一个指定排 ...

  6. HDU-1263(STL+排序)

    水果 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...

  7. (C++)STL排序函数sort和qsort的用法与区别

    主要内容: 1.qsort的用法 2.sort的用法 3.qsort和sort的区别 qsort的用法: 原 型: void qsort(void *base, int nelem, int widt ...

  8. CodeForces 558E(计数排序+线段树优化)

    题意:一个长度为n的字符串(只包含26个小字母)有q次操作 对于每次操作 给一个区间 和k k为1把该区间的字符不降序排序 k为0把该区间的字符不升序排序 求q次操作后所得字符串 思路: 该题数据规模 ...

  9. c++STL排序算法注意事项

    关于算法中的比较函数 #include<iostream> #include<algorithm> using namespace std; int compare(doubl ...

随机推荐

  1. Intellij IDEA 修改代码后自动编译更新

    Intellij IDEA 一些不为人知的技巧 问题描述: Intellij IDEA 调试修改时,改动页面和 java 文件后,无法立刻看到变化,需要手动重启服务. 问题原因: 在 IDEA tom ...

  2. Java GET和POST请求

    从表面来看GET和POST请求: GET请求是在url后直接附上请求体,url和请求体之间用"?"分割,不同参数之间用"&"分隔,%XX中的XX为该符号 ...

  3. PHP Json函数不能处理中文的解决办法

    PHP5.2 新增的 json 功能是非常受欢迎的,但是经过测试发现,json_encode 对中文的处理是有问题的: 不能处理GB编码,所有的GB编码都会替换成空字符: utf8编码的中文被编码成u ...

  4. linux应用之vsftp服务的安装及配置(centos)

    1.centos中vsftp服务的安装 方法1:rpm方式 #rpm –ivh vsftpd-2.0.5-10.el5.i386.rpm  安装rpm程序包(网上下载的rpm包) 方法2:yum方式 ...

  5. 【应用】SVG动态 时钟

    没有做秒针,只做了分针和时针,5分钟以后来看应该可以看到效果╮(╯-╰)╭ <!DOCTYPE html> <html> <head> <title>& ...

  6. Rsync+Sersync同步

    Rsync+Sersync同步特点: (1):sersync可以记录下被监听目录中发生变化的(包括增加.删除.修改)具体某一个文件或某一个目录的名字:(2):rsync在同步的时候,只同步发生变化的这 ...

  7. iptables 端口映射

    一.环境和要实现功能 PC1的网络设置如下: eth0       192.168.0.29 内网 eth1 219.239.11.22 外网 PC2的网络设置则为:192.168.0.21 内网 我 ...

  8. 五 pyJWT使用

    PyJWT是一个Python库,用来编码/解码JWT(JSON Web Token)的. 1:安装PyJWT 2:  直接上代码了: import datetime, jwt, time from a ...

  9. redis 操作使用

    /*1.Connection*/ $redis = new Redis(); $redis->connect('127.0.0.1',6379,1);//短链接,本地host,端口为6379,超 ...

  10. 你忘记的java运算符

    当整数被0除时会得到一个无穷大,或者nan, 所以会抛出数据溢出的异常.