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 ...
随机推荐
- bootstrap:图片轮播
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- Redis入门--1.安装Redis
redis是什么? 是完全开源免费的,用c语言编写的,是一个单线程,高性能的(key/value)内存数据库,基于内存运行并支持持久化的nosql数据库 redis能干嘛? 主要是用来做缓存,但不仅仅 ...
- 每日一问2:堆(heap)和栈(stack)的区别
因为这里没有明确指出堆是指数据结构还是存储方式,所以两个尝试都回答一下. 一.堆和栈作为数据结构 1.堆(heap),也叫做优先队列(priority queue),队列中允许的操作是先进先出(FIF ...
- Match3 Module For Game(THDN)
介绍 THDN的核心机制为Match3的利用,本文对Match3 Gameplay进行记录,并对其进行改良.THDN作为RogueLIke性质的游戏,玩家在随机生成的dungeon里进行探索并获 ...
- BeanUtils 如何拷贝 List?
BeanUtils 如何拷贝 List? 一.背景 我们在DO.Model.VO层数据间可能经常转换数据: Entity对应的是持久层数据结构(一般是数据库表的映射模型); Model 对应的是业务层 ...
- GDAL集成GEOS
因为要用到缓冲区分析,在使用Buffer的时候提示:ERROR 6: GEOS support not enabled,查了一下资料需要集成GEOS库.因为GDLA默认编译是没有集成GEOS库的. 现 ...
- 低秩稀疏矩阵恢复|ADM(IALM)算法
一曲新词酒一杯,去年天气旧亭台.夕阳西下几时回? 无可奈何花落去,似曾相识燕归来.小园香径独徘徊. ---<浣溪沙·一曲新词酒一杯>--晏殊 更多精彩内容请关注微信公众号 "优化 ...
- 矩形内的递推dp
链接:https://www.nowcoder.com/acm/contest/130/B来源:牛客网 黑妹和黑弟又聚在一起玩游戏了,这次他们选择在一个n*m的棋盘上玩游戏,棋盘上的每个方格都有一个非 ...
- 使用git将本地文件提交到github存储库
1.首先你要安装git https://git-for-windows.github.io/ 去官网自行下载对应版本 2.安装好git服务器后,找到你项目的文件夹,右键git bash here打开命 ...
- linux下 Error in 'python3':free(): invalid pointer
linux下坑人的报错!折腾了好久. 现象:这次是一个底层库 C++,底层库之上一层SDK C++,之上再一层so库,用python调用SDK.然后python层依赖了opencv和SDK,调换ope ...