Codeforces 997D(STL+排序)
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:
- divide the number x by 3 (x must be divisible by 3);
- 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.
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⋅10^18) — rearranged (reordered) sequence that Polycarp can wrote down on the board.
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.
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=9.
概译:对一个数进行连续变换,要么除以3要么乘2,比如这个数是126,除以3是42,乘2是84,除以3是28。这个除以3必须是3的整倍数,且除以3和乘2选择哪个都可以没有顺序要求。这样这个序列A为:126,42,84,28。输入文件会给出一个乱序的B比如:42,28,84,126,然后我们来找出原来的序列A并输出。
思路:这是一个div3的题目,div3是新开的面向入门选手的比赛模式,大家可以尝试一下。AlphaWA写这个题时用了类似搜索的方式,先随便找个作为标杆的数(比如我直接用了a[1]),然后所求序列中在它之前的数储存在一个栈中,在它之后的储存在一个队列中,最后先后输出栈和队列中的数即可。
(以前太懒了这次帮忙画个图?(`・ω・´))

代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;//3*10^18已经爆int,需要longlong map<ll,bool>m;//map记录是否存在某个数
stack<ll>s;
queue<ll>q; int main()
{
int n;
ll a[105];
//输入
scanf("%d",&n);
for(int i=1;i<=n;i++) cin>>a[i],m[a[i]]=true;
//用栈储存在a[1]前面的
s.push(a[1]);
ll k=s.top();
do
{
if(m[k*3]) s.push(k*3),k*=3;
else if(k%2==0&&m[k/2]) s.push(k/2),k/=2;
else break;
}while(true);
//用队列储存在a[1]后面的
k=a[1];
do
{
if(k%3==0&&m[k/3]) q.push(k/3),k/=3;
else if(m[k*2]) q.push(k*2),k*=2;
else break;
}while(true);
//输出
while(s.size())
{
cout<<s.top()<<' ';
s.pop();
}
while(q.size())
{
cout<<q.front()<<' ';
q.pop();
} return 0;
}
而官方题解就比较简单了,用了pair的排序特性,以前我们写过的一道cf上的div2的题也是巧用了pair的排序特性,即:先排first后排second。其实不用pair自己自定义sort的比较函数也ok只是正好两个元素没这个方便。
这些数可以分为一些种类:能除以3且能连除n次的,连除n-1次的,……1次的,0次的。举样例:

详见代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL; int count3(LL x){
int ret=;
while(x % == ){
ret++;
x /= ;
}
return ret;
} int n;
vector<pair<int,LL> > v; int main(){
cin>>n;
v.resize(n);//设置vector大小为n for(int i=; i<n; i++){
cin>>v[i].second;
v[i].first=-count3(v[i].second);//这里要count3更大的数排前面,所以取负
} sort(v.begin(), v.end()); for(int i=; i<n; i++)
printf("%lld%c", v[i].second, " \n"[i + == n]);//学下这波语法 return ;
}
可以无视的后话:经常有同学问C和C++的事情,这道题就疯狂使用了STL,想参加算法竞赛的同学要抓紧学一下STL了,很方便哒!
Codeforces 997D(STL+排序)的更多相关文章
- 详细解说 STL 排序(Sort)
0 前言: STL,为什么你必须掌握 对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算 ...
- 转:详细解说 STL 排序(Sort)
详细解说 STL 排序(Sort) 详细解说 STL 排序(Sort) 作者Winter 详细解说 STL 排序(Sort) 0 前言: STL,为什么你必须掌握 1 STL提供的Sort 算法 1. ...
- 详细解说 STL 排序(Sort)(转)
作者Winter 详细解说 STL 排序(Sort) 0 前言: STL,为什么你必须掌握 1 STL提供的Sort 算法 1.1 所有sort算法介绍 1.2 sort 中的比较函数 1.3 sor ...
- AIM Tech Round 4 (Div. 2)(A,暴力,B,组合数,C,STL+排序)
A. Diversity time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...
- STL 排序(转载)
这篇文章关于STL中的排序写的虽不深入,但是还是挺好的. 1.sort sort有两种形式,第一种形式有两个迭代器参数,构成一个前开后闭的区间,按照元素的 less 关系排序:第二种形式多加一个指定排 ...
- HDU-1263(STL+排序)
水果 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...
- (C++)STL排序函数sort和qsort的用法与区别
主要内容: 1.qsort的用法 2.sort的用法 3.qsort和sort的区别 qsort的用法: 原 型: void qsort(void *base, int nelem, int widt ...
- CodeForces 558E(计数排序+线段树优化)
题意:一个长度为n的字符串(只包含26个小字母)有q次操作 对于每次操作 给一个区间 和k k为1把该区间的字符不降序排序 k为0把该区间的字符不升序排序 求q次操作后所得字符串 思路: 该题数据规模 ...
- c++STL排序算法注意事项
关于算法中的比较函数 #include<iostream> #include<algorithm> using namespace std; int compare(doubl ...
随机推荐
- Android 启动过程介绍【转】
本文转载自:http://blog.csdn.net/yangwen123/article/details/8023654 一般开机过程大致可以分为三个大阶段: 1. OS级别,由bootloader ...
- apache web 服务器
0. 特性与特点 性能方面,apache 在设计时采用了以"进程"为基础的结构,自然进程比线程消耗了更多的系统开销,导致了 apache 在多处理器环境中性能有所下降: 因此,在对 ...
- 解决Exception:Could not open Hibernate Session for transaction; nested exception is java.lang.NoClassDefFoundError: org/hibernate/engine/transaction/spi/TransactionContext
原因是配置文件中 <bean id="transactionManager" class="org.springframework.orm.hibernate4.H ...
- bzoj 2151 种树——贪心+后悔
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2151 似乎是半年+前讲过的.(然而看到的时候却不会了) 考虑贪心,限制就是不能选两边的.如果 ...
- bzoj 4289 PA2012 Tax——构图
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4289 可以把一个点上的边按权值排序,然后边权小的向第一个比它大的连差值的边,边权大的向第一个 ...
- zlog日志库的简单封装,以及给debug级别添加颜色显示
现看看效果如何: 方法如下: 定义相关颜色的宏 #define ESC_START "\033[" #define ESC_END "\033[0m" #def ...
- PICO SCOPE 3000 Series 示波器
- Android 常用adb shell 命令(转)
调试Android程序有时需要adb shell 命令,adb全称Android Debug Bridge ,就是起到调试桥的作用. 通过adb我们可以在Eclipse中通过DDMS来调试Androi ...
- dockerfile创建镜像(二)
ENTRYPOINT 两种格式: ENTRYPOINT [“executable”, “param1”, “param2”] ENTRYPOINT command param1 param2 (she ...
- androidEditTextinputType及android:imeOptions=”actionDone”(转)
一.android 软件盘事件响应 在android中,有时需要对EditText实现软件盘监听的场景.当android按下软键盘的时候,响应完成.发送.搜索或者其他事件. Google 提供了 Ed ...