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 位选手, 第 ...
 
随机推荐
- 正方形(类型:枚举、一级、C++)
			
题目描述 有一个正方形,四个角的坐标分别是(1,-1),(1,1),(-1,-1),(-1,1).写一个程序,判断一个给定的点(x,y)是否在这个正方形内(包括正方形边界),如果在正方形内输出“Yes ...
 - 【六】tf和cgi进行联合试验,完成日志服务器
			
[任务6]tf和cgi进行联合试验,完成日志服务器 [任务6]tf和cgi进行联合试验,完成日志服务器 改装gen-cpp目录下client.cpp文件 启动Nginx服务和gen-cpp目录下编译后 ...
 - linux-2.6.22.6内核启动分析之Makefile文件
			
学习目标 分析Makefile文件,了解内核中的哪些文件被编译,如何被编译,连接时顺序如何确定! Linux内核源码中包含很多的Makefile文件,这些Makefile文件又包含其它的一些文件,比如 ...
 - 基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(一)之miscdevice和ioctl
			
基于OMAPL138的Linux字符驱动_GPIO驱动AD9833(一)之miscdevice和ioctl 0. 导语 在嵌入式的道路上寻寻觅觅很久,进入嵌入式这个行业也有几年的时间了,从2011年后 ...
 - jenkins + ansible + docker 代码集成发布
			
一.环境搭建 1. 安装Java 配java_home, /etc/profile 2.安装Jenkins 下载war包,用 Java -jar Jenkins.war或者 把war包放tomca ...
 - 在Ubuntu上开启MongoDB的IP Security
			
本文翻译之MongoDB官网博客,原地址:https://www.mongodb.com/blog/post/enabling-ip-security-for-mongodb-36-on-ubuntu ...
 - centos系统误删libc.so.6
			
前段时间遇到开发人员更新glibc版本,把/usr/lib64/libc-2.12.so & libc.so.6 -> libc-2.12.so 这个软连接更改之后导致报错: ls: e ...
 - Bessel函数的零点计算  MATLAB
			
由于MATLAB自己没有附带贝塞尔函数零点,因此使用起来很不方便,特别是在绘制仿真场量时. 下面给出0-9阶的贝塞尔函数零点的计算公式,其中理论上计算零点个数N在50以内时较为精确: function ...
 - Nexus Repository3安装和maven,npm配置(Linux)
			
Nexus Repository下载 根据操作系统选择指定版本,本文针对Linux安装,其他的安装过程可能有所差异. https://help.sonatype.com/repomanager3/do ...
 - 搜索引擎的选择与在chrome上的设置
			
1 优缺点分析 百度:广告多,但是电脑端可以用Adblock Plus屏蔽:搜索内容有很多百度自家内容,如百家号.百度知道.百度文库.百度贴吧等,在搜索教程的时候很实用,但是不适合偏专业性搜索,很多 ...