Codeforces 997D(STL+排序)

                   D. Divide by three, multiply by two
                      time limit per test 1 second
                    memory limit per test 256 megabytes
                        input:standard input
                        output: standard output

Polycarp likes to play with numbers. He takes some integer number x, writes it down on the board, and then performs with it n1 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.

Input

The first line of the input contatins an integer number n (2n100) — the number of the elements in the sequence. The second line of the input contains n integer numbers a1,a2,,an (1ai310^18) — 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
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][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=0;
  while(x % 3 == 0){
    ret++;
    x /= 3;
  }
  return ret;
}

int n;
vector<pair<int,LL> > v;

int main(){
  cin>>n;
  v.resize(n);//设置vector大小为n
  
  for(int i=0; i<n; i++){
    cin>>v[i].second;
    v[i].first=-count3(v[i].second);//这里要count3更大的数排前面,所以取负 
  }
  
  sort(v.begin(), v.end());
  
  for(int i=0; i<n; i++)
    printf("%lld%c", v[i].second, " \n"[i + 1 == n]);//学下这波语法 
    
  return 0; 
}

可以无视的后话:经常有同学问C和C++的事情,这道题就疯狂使用了STL,想参加算法竞赛的同学要抓紧学一下STL了,很方便哒!

猜你喜欢

转载自www.cnblogs.com/AlphaWA/p/9158360.html
今日推荐