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 位选手, 第 ...
随机推荐
- Extjs 中callParent的作用
callParent 是 Sencha 类系统提供的一个用于调用你父/祖先类中的方法. 这个通常用于当你 继承一个框架类 或者 覆写一个类中提供的方法(比如 onRender) 时. 当你在一个带参数 ...
- (二)ubuntu下安装Amd RX470驱动
0X:ADM官方下载驱动 https://www.amd.com/en/support 查看本机驱动命令 lspci | grep -i vga 选择自己的驱动 下载对应的版本 现在最新的是:amdg ...
- PHP程序员学Objective-C之后的变化
趣味坎谈,不一定100%准确,以自己的实际情况为准; 如题,我2008年开始学PHP,PHP是我学的第二门编程语言,一直用到现在,2010年初开始做iOS开发,学习了Objective-C,学这2门语 ...
- 课时14.DTD文档声明上(掌握)
1.什么是DTD文档声明? 由于HTML有很多格版本的规范,每个版本的规范之间又又一些差异,所以为了让浏览器能够正确的编译/解析/渲染我们的网页,我们需要在HTML文件的第一行告诉浏览器,我们当前这个 ...
- 大数据学习--day14(String--StringBuffer--StringBuilder 源码分析、性能比较)
String--StringBuffer--StringBuilder 源码分析.性能比较 站在优秀博客的肩上看问题:https://www.cnblogs.com/dolphin0520/p/377 ...
- spring boot2整合dubbox全注解
前题 dubbox是dubbo的一个升级版,简单说就是本来dubbo是阿里开发的,现在阿里不维护了,当当网拿过去继续开发.本来阿里的dubbo维护到2.6版本,而再高版本的都是当当网维护的就叫成dub ...
- Python学习 :异常处理
异常处理 什么是异常处理 - python解释器检测到错误,触发异常(也允许程序员自己触发了异常) - 程序员编写特定的代码,专门用来捕捉这个异常(这段代码与程序逻辑无关,只与异常处理有关) - 如果 ...
- go学习之文件读取问题(需更新)
go学习之文件读取问题(需更新) 一,问题: 今天做一个GO广度迷宫问题时,出现了一个奇怪的问题. 就是通过fmt.Fscanf()读取文件时,每行末尾多出一个0. 二,问题代码: 1,输入文件maz ...
- 2017-2018-1 20155321 《信息安全系统设计基础》课堂实践——实现mypwd
2017-2018-1 20155321 <信息安全系统设计基础>课堂实践--实现mypwd 学习pwd命令 pwd命令:输出当前工作目录的绝对路径 还可通过man pwd具体查看pwd的 ...
- 20145209 2016-2017-2 《Java程序设计》课堂实践内容
20145209 2016-2017-2 <Java程序设计>课堂实践内容 一.递归 题目详情: public class TestArgs{ public static void mai ...