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, and then performs with it n−1n−1operations of the two kinds:
- divide the number xx by 33 (xx must be divisible by 33);
- multiply the number xx by 22.
After each operation, Polycarp writes down the result on the board and replaces xx by the result. So there will be nn numbers on the board after all.
You are given a sequence of length nn — 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.
The first line of the input contatins an integer number nn (2≤n≤1002≤n≤100) — the number of the elements in the sequence. The second line of the input contains nn integer numbers a1,a2,…,ana1,a2,…,an (1≤ai≤3⋅10181≤ai≤3⋅1018) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
Print nn 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.
6
4 8 6 3 12 9
9 3 6 12 4 8
4
42 28 84 126
126 42 84 28
2
1000000000000000000 3000000000000000000
3000000000000000000 1000000000000000000
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=9x=9.
题目大意:给定的数组按照以下要求排序:后一个数是前一个数的三分之一,或者是前一个数的二倍。
思路:如果a[v]是a[u]的三分之一或者二倍,就给u->v加一条有向边,然后跑一遍拓扑排序就行了,注意得到的拓扑数组是下标
代码:
#include<cstdio>
#include<iostream>
#include<string>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<vector>
#include<map>
typedef long long ll;
using namespace std;
const int maxn = 200000 + 100;
int G[100 +10][100 + 10];
int c[maxn];
ll topo[maxn], t;
int n;
bool dfs(int u){
c[u] = -1;
for(int i = 0; i < n; i++)if(G[u][i]){
if(c[i] < 0)return false;
else if(!c[i] && !dfs(i))return false;
}
c[u] = 1;topo[--t] = u;
return true;
}
bool toposort(){
t = n;
memset(c, 0, sizeof(c));
for(int i = 0; i < n; i++)if(!c[i]){
if(!dfs(i)) return false;
}
return true;
}
int main(){
scanf("%d", &n);
ll a[maxn];
memset(G, 0, sizeof(G));
for(int i = 0; i < n; i++){
scanf("%lld", &a[i]);
}
sort(a, a+n);
for(int i = 0; i < n ; i++){
int it = lower_bound(a, a+n, a[i]/3)-a;
int itt = lower_bound(a, a+n, a[i]*2)-a;
if(a[i]%3==0&&a[it]==a[i]/3)G[i][it] = 1;
if(a[i]*2==a[itt])G[i][itt] = 1;
}
toposort();
for(int i = 0; i < n; i++)printf("%lld ", a[topo[i]]);
}
Codeforces 977D Divide by three, multiply by two(拓扑排序)的更多相关文章
- Codeforces Global Round 8 E. Ski Accidents(拓扑排序)
题目链接:https://codeforces.com/contest/1368/problem/E 题意 给出一个 $n$ 点 $m$ 边的有向图,每条边由编号较小的点通向编号较大的点,每个点的出度 ...
- codeforces 645 D. Robot Rapping Results Report 二分+拓扑排序
题目链接 我们可以发现, 这是一个很明显的二分+拓扑排序.... 如何判断根据当前的点, 是否能构造出来一个唯一的拓扑序列呢. 如果有的点没有出现, 那么一定不满足. 如果在加进队列的时候, 同时加了 ...
- codeforces 638B—— Making Genome in Berland——————【类似拓扑排序】
Making Genome in Berland time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 【CodeForces 129 B】Students and Shoelaces(拓扑排序)
Anna and Maria are in charge of the math club for junior students. When the club gathers together, t ...
- Codeforces Round #460 (Div. 2)_D. Substring_[dp][拓扑排序]
题意:一个有向图,每个结点 被赋予一个小写字母,一条路径的value等与这条路径上出现次数最多的字母的数目,求该图的最大value 比赛时,用dfs超时,看官方题解用的dp和拓扑排序,a--z用0-2 ...
- 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= ...
- codeforces 792C. Divide by Three
题目链接:codeforces 792C. Divide by Three 今天队友翻了个大神的代码来问,我又想了遍这题,感觉很好,这代码除了有点长,思路还是清晰易懂,我就加点注释存一下...分类吧. ...
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序
B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...
- Codeforces Beta Round #29 (Div. 2, Codeforces format) C. Mail Stamps 离散化拓扑排序
C. Mail Stamps Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem ...
随机推荐
- mybatis两种方式
// 1.传统的使用 statementId方式 //获取详情 sysApiDocumentMode = template.selectOne("oaApiDocument.getProje ...
- javaScript类型和对象
javaScript基本数据类型 Undefined: Null: Boolean: String: Number: Symbol: Object. 注意 JavaScript 的代码 undefin ...
- 测开大佬告诉你:如何5分钟快速创建restful风格的API接口-使用django restframework框架
一.思考❓❔ 1.创建API接口难吗? 软件测试工程师: 只测过API接口, 从没创建过 应该需要掌握一门后端开发语言和后端开发框架吧!? 脑容量有限,想想就可怕 2.如何创建API接口呢? 使用Dj ...
- java反序列化php序列化的对象
最近工作中遇到一个需要解析php序列化后存入DB的array, a:4:{i:0;a:2:{s:11:"province";s:8:"0016";s:7:&qu ...
- git 远程托管
1.创建别名 git remote add orgin(别名) url 2.推入云端 git push 别名 master(分支) git push 别名 dev 3.克隆(默认只有master分支) ...
- Java单体应用 - 开发工具 - 02.Maven
原文地址:http://www.work100.net/training/monolithic-tools-maven.html 更多教程:光束云 - 免费课程 Maven 序号 文内章节 视频 1 ...
- SpringMVC简单使用教程
一.SpringMVC简单入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 <!--conf ...
- Linux起源
Linux起源 操作系统出现时间线: Unix1970年诞生 ,71年用C语言重写 Apple II 诞生于1976年 window诞生于1985年 Linux诞生于1991年,由大学生Linus T ...
- 关于PDF阅读器
获取流程 1.点击下载xodo 2.跳转到如下界面,单击箭头所指的版本: 3.单击转到 中国-中文 4.点击获取 5.在跳出来的界面点击红框 6.打开本机的Microsoft Store下载应用 介绍 ...
- POJ Expanding Rods
点击打开题目 题目大意 给定L,n,C,L为红色线段,L(1+n*C)为绿色弧,求两者中点的距离 二分圆心角度数,接下来就是几何的能力了 根据正弦定理,可得: Lsinθ=rsin(90°−θ) 则弧 ...