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 位选手, 第 ...
随机推荐
- python3爬虫-通过selenium获取TB商品
from selenium import webdriver from selenium.webdriver.support.wait import WebDriverWait from seleni ...
- 蓝桥杯第七届决赛(国赛)C++B组 第四题 机器人塔
机器人塔 X星球的机器人表演拉拉队有两种服装,A和B.他们这次表演的是搭机器人塔. 类似: A B B A B A A A B B B B B A BA B A B B A 队内的组塔规则 ...
- windows安装Oracle数据库
我装的版本是Oracle11,64位,直接网上下载即可.安装过程中也出现了一些坑,现在整理了一下. 1.下载的目录和安装的目录最好放到英文目录下,别放到中文或者特殊字符的文件夹中,点击setup.ex ...
- javascript json对象操作(基本增删改查)
/** * Json对象操作,增删改查 * * @author lellansin * @blog www.lellansin.com * @version 0.1 * * 解决一些常见的问题 * g ...
- mybatis中SQL语句运用总结
union 连接查询 连接两个表后会过滤掉重复的值 <resultMap id="BaseResultMap" type="com.sprucetec.pay.e ...
- Role Helper
using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using System.Collections.Ge ...
- 大数据IDEA调试flink程序
Flink在IDEA中开发是一件比较困难的事情,网上没有参考资料,就算就业说的太过笼统,不知道是会了不说还是不会瞎说,为了解决flink这个问题,本人特别做了一遍开发的简单说明.主要考虑两个问题,1. ...
- sqli-labs(less-11-16)
POST登入 首先试试 uname=admin'# & passwd=1 登入成功 如果不知道用户名 ,注释符被过滤,可以从password入手 一般第一个登陆字段(一般是用户名)就用注释,第 ...
- Vue2.5入门-3
安装和使用 全局安装vue npm install --global vue-cli 创建基于webpack模板的新项目 vue init webpack my-project 安装依赖 cd my- ...
- Go类型特性-学习笔记
1.组合 Go语言使用组合来完成类型的设计,设计某一类型时想要拥有其他类型的功能只需要将其他类型嵌入该类型即可. 2.接口 与其他语言不同的是,编译器会自动判断该类型是否符合某正在使用的接口,甚至不需 ...