Divide by three, multiply by two(DFS+思维)
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:
1.divide the number x by 3 (x must be divisible by 3);
2.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
Copy
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.
题目意思:给你n个数,让你将这些数排序,后一个数应该是前一个数的二倍或 三分之一(必须能被三整除),输出任意一种结果,数据保证有解。
解题思路:使用DFS深搜,n个点,如果点j的权重是点i的二倍或三分之一, 那么点 i 到 点 j 有一条单向边 。最终的排序结果,也就是遍历所有点的任意一种方式。
#include<cstdio>
#include<map>
#include<algorithm>
#define ll long long int
using namespace std;
int n,k,flag;
int counts;
map<ll,int>mp;
ll b[];
void DFS(ll x)
{
if(counts==n-)
{
flag=;
return ;
}
if(x%==&&mp[x/]==)
{
counts++;
b[counts]=x/;
DFS(x/);
}
if(mp[x*]==)
{
counts++;
b[counts]=x*;
DFS(x*);
}
return ;
}
int main()
{
int i;
ll a[];
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%lld",&a[i]);
mp[a[i]]=;
}
flag=;
for(i=;i<n;i++)
{
counts=;
b[]=a[i];
DFS(a[i]);
if(flag)
{
for(i=;i<n;i++)
{
if(i==n-)
{
printf("%lld\n",b[i]);
}
else
{
printf("%lld ",b[i]);
}
}
break;
}
}
return ;
}
对于这一道题其实还有另外一种方法,自定义sort的排序方式我们试分析。对于这个序列,因为3这个因数是要被除的,因此在整个序列中,3的个数必定是逐渐减少的。因此我们可以先统计所有数的3的个数,然后根据3的个数进行sort排序。而对于3的个数相同的时候,此时意味着不能进行除以3的操作,即只能进行*2的操作,因此,我们只需要将大的数排在后面即可。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define LL long long int
using namespace std;
int my_cmp(LL a,LL b)
{
int count_a,count_b;
count_a=;
count_b=;
while(a%==)
{
a=a/;
count_a++;
}
while(b%==)
{
b=b/;
count_b++;
}
if(count_a==count_b)
{
if(a<b)
{
return ;
}
else
{
return ;
}
}
else if(count_a>count_b)
{
return ;
}
else if(count_a<count_b)
{
return ;
}
}
int main()
{
LL a[];
int i,n;
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%lld",&a[i]);
}
sort(a,a+n,my_cmp);
for(i=;i<n;i++)
{
printf("%lld%c",a[i],i==n-?'\n':' ');
}
return ;
}
Divide by three, multiply by two(DFS+思维)的更多相关文章
- Codeforces Round #479 (Div. 3) D. Divide by three, multiply by two (DFS)
题意:给你一个长度为\(n\)的序列\(a\).对它重新排列,使得\(a_{i+1}=a_{i}/3\)或\(a_{i+1}=2*a_{i}\).输出重新排列后的序列. 题解:经典DFS,遍历这个序列 ...
- hdu6035[dfs+思维] 2017多校1
/*hdu6035[dfs+思维] 2017多校1*/ //合并色块, 妙啊妙啊 #include<bits/stdc++.h> using namespace std; ; const ...
- Codeforces Round #479 (Div. 3) D. Divide by three, multiply by two
传送门 D. Divide by three, multiply by two •题意 给你一个数 x,有以下两种操作,x 可以任选其中一种操作得到数 y 1.如果x可以被3整除,y=x/3 2.y= ...
- D. Eternal Victory(dfs + 思维)
D. Eternal Victory time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Divide by three, multiply by two CodeForces - 977D (思维排序)
Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, a ...
- Codeforces 977D Divide by three, multiply by two(拓扑排序)
Polycarp likes to play with numbers. He takes some integer number xx, writes it down on the board, ...
- HDU 6060 RXD and dividing(dfs 思维)
RXD and dividing Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Other ...
- hiboCoder 1041 国庆出游 dfs+思维
先抽象出一棵以1做为根结点的树.给定了访问序列a[1..m]. 考虑两种特殊情况: 1.访问了某个a[j],但是存在a[i]没有访问且i < j,出现这种情况说明a[j]一定是a[i]的祖先节点 ...
- newcoder F石头剪刀布(DFS + 思维)题解
题意:wzms 今年举办了一场剪刀石头布大赛,bleaves 被选为负责人. 比赛共有 2n 个人参加, 分为 n 轮, 在每轮中,第 1 位选手和第 2 位选手对战,胜者作为新的第 1 位选手, 第 ...
随机推荐
- 【css】 如何修改select的样式
select { /*清除select默认样式*/ appearance:none; -moz-appearance:none; -webkit-appearance:none; -ms-appear ...
- CentOS6安装各种大数据软件 第八章:Hive安装和配置
相关文章链接 CentOS6安装各种大数据软件 第一章:各个软件版本介绍 CentOS6安装各种大数据软件 第二章:Linux各个软件启动命令 CentOS6安装各种大数据软件 第三章:Linux基础 ...
- activemq的高级特性:消息的可靠性
高级特性之消息的可靠性 可靠性分为:生产者.消费者.生产者必须让mq收到消息,消费者必须能够接收到消息并且消费成功,这就是消息的可靠性. 1:生产者可靠性 Session session = conn ...
- easyui图标
只要在icons属性上,加上图标对应的名字,easyUI就会显示对应的图标,这些图标都是easyui内置的.
- Delphi跨平台下的GetTickCount,GetCurrentThreadID
在Windows下只要uses Windows,就有这两个API可调用GetTickCount,GetCurrentThreadID 如果我们需要跨平台使用这两个函数,就不能仅仅Uses Window ...
- CAP通俗解释
CAP原则又称CAP定理,指的是在一个分布式系统中,Consistency(一致性). Availability(可用性).Partition tolerance(分区容错性),这三个基本需求,最多只 ...
- micro:bit 软件生态系统介绍
microbit 软件分成在microbit (Target Computer 如下图右边)上执行的及主计算机(Host Computer 如下图左边)上两类 : 一般程序写好后透过USB 转到mic ...
- docker inspect获取详细参数的两种方法
docker inspect xx 返回的是一个json格式的数据 以下为部分返回值 [ { "Id": "706813b0da107c4d43c61e3db9da908 ...
- 【原创】frozenset集合函数入门及实例
函数作用 frozenset() 返回一个冻结的集合,冻结后集合不能再添加或删除任何元素.与之对应的是set函数,set无序排序且不重复,是可变的,有add(),remove()等方法. 函数原型 f ...
- golang基础--Gocurrency并发
Go并发特点 goroutine只是由官方实现的超级"线程池"而已,每个实例4-5kb的栈内存占用和用于实现机制而大幅减少的创建和销毁开销. 并发不是并行(多CPU): Concu ...