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 ...
随机推荐
- 天天都在用Git,那么你系统学习过吗?(一)学习过程
你系统学习Git了吗? 使用Mac编程的好处,不是因为Mac长得好看 Git内容学习准备 如果你还没有用Git,就不要写代码了. GitHub仓库的使用. 新员工入职的时候,会让他先用一周的时间去学习 ...
- 如何修改Docker已运行实例的端口映射
如何修改Docker已运行实例的端口映射 Docker的端口映射,往往出现在两个阶段需要处理: 1.是在docker启动前就已经确定好,哪个docker实例映射哪个端口(往往这个情况比较,需要提前做规 ...
- Ubuntu1804下安装Gitab
部署gitlab 1.配置仓库源 # vim /etc/apt/sources.listdeb http://mirrors.aliyun.com/ubuntu/ bionic main restri ...
- C#实现把查询出的Table作为参数更新到数据库
1.ImportData主方法 把传入为object数组类型,按照下标取出对应的参数,此处为Table和Username public object[] ImportData(object[] Par ...
- log日志拦截
简介 主要记录一下项目中的日志拦截和异常拦截,由于之前公司项目为单体项目,所使用的日志拦截较为简单,只是用拦截器进行前后对日志的拦截,异常拦截直接使用@ExceptionHandler,而现在公司接口 ...
- 2019CSP复赛游记
Day 0 作为一个初三的小蒟蒻…… 什么算法都不会打…… 做一道LCA+生成树的图论题调了两个小时…… 明日裸考…… Day 1 Morning 买了两个士力架,带了一盒牛奶,准备在考场上食用(这个 ...
- (分块暴力)Time to Raid Cowavans CodeForces - 103D
题意 给你一段长度为n(1 ≤ n ≤ 3·1e5)的序列,m (1 ≤ p ≤ 3·1e5)个询问,每次询问a,a+b,a+2b+...<=n的和 思路 一开始一直想也想不到怎么分,去维护哪些 ...
- 在eclipse里用jdbc连接MySQL
进入MySQL控制台, 输入密码, 新建数据库test1并给用户授权,用户名“jaovo”, 创建表,id主键自增, 下载jdbc驱动包(jar文件) 把它放进tomcat的安装目录lib文件夹下(我 ...
- django cms 5月第一弹
官方文档: ##http://django-cms.readthedocs.io/en/latest/index.html #截图 #生存的项目结构
- 钝化 会钝化 订单审批流程 码一会er
先放一张订单审批流程图.预则立嘛