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. javascript学习第一天

    从大学第一次接触到JavaScript,到现在一直是个软肋,之前也是学习过一遍,但是缺乏系统学习,基础不牢,那么今天开始从基础部分学起来,今天是第一天,每天至少要保证效率,也要保证学习质量. 恩,要按 ...

  2. BZOJ2120 数颜色 —— 待修改莫队

    题目链接:https://vjudge.net/problem/HYSBZ-2120 2120: 数颜色 Time Limit: 6 Sec  Memory Limit: 259 MBSubmit:  ...

  3. 谈谈嵌套for循环的理解

    谈谈嵌套for循环的理解     说for的嵌套,先说一下一个for循环的是怎么用的.      这次的目的是为了用for循环输出一个乘法口诀表,一下就是我的一步步理解.    一.   语法:   ...

  4. BZOJ_3781_小B的询问_莫队

    BZOJ_3781_小B的询问_莫队 Description 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值 ...

  5. Nagios监控Windows的网卡流量

    Nagios监控Windows的网卡流量 使用/usr/local/nagios/libexec/中的check_traffic.sh,不但可以监控Linux的网卡流量,也可以监控Windows服务器 ...

  6. 利用union判断系统的大小端

    int checkCPUendian()//返回1,为小端:反之,为大端: { union { unsigned int a; unsigned char b; }c; c.a = 1; return ...

  7. Android开发---开发文档翻译

    2014.11.24 1:ClipData类:用于表示剪切的数据,此剪切的数据可以是复杂类型,包括一个或多个条目实例 (1)基础知识 >公共类:public class >嵌套类:Clip ...

  8. nohup、&、tail 在服务启动中的用法

    在利用命令行启动各类服务的时候,控制台一般会显示相关日志信息,如果weblogic的启动,我们可能有这样需求:启动服务器后即使关闭控制服务器依然运行 ,日志不但可以在控制台显示同时也记录里后台日志文件 ...

  9. 5、html的body内标签之多行文本及下拉框

    一.多行文本 <textarea name="">默认值</textarea> 二.下拉框 1.单选 <select name="city& ...

  10. 技术胖Flutter第四季-23静态资源和项目图片的处理

    技术胖Flutter第四季-23静态资源和项目图片的处理 视频地址:https://www.bilibili.com/video/av35800108/?p=24 项目中引用图片静态资源文件 这里就是 ...