zoukankan      html  css  js  c++  java
  • Divide by three, multiply by two(DFS+思维)

    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:

        1.divide the number x by 3 (x must be divisible by 3);
        2.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(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⋅1018) — 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
    Copy

    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]. It can match possible Polycarp's game which started with x=9.

     题目意思:给你n个数,让你将这些数排序,后一个数应该是前一个数的二倍或 三分之一(必须能被三整除),输出任意一种结果,数据保证有解。

     解题思路:使用DFS深搜,n个点,如果点j的权重是点i的二倍或三分之一, 那么点 i  到 点 j 有一条单向边 。最终的排序结果,也就是遍历所有点的任意一种方式。

     1 #include<cstdio>
     2 #include<map>
     3 #include<algorithm>
     4 #define ll long long int
     5 using namespace std;
     6 int n,k,flag;
     7 int counts;
     8 map<ll,int>mp;
     9 ll b[110];
    10 void  DFS(ll x)
    11 {
    12     if(counts==n-1)
    13     {
    14         flag=1;
    15         return ;
    16     }
    17     if(x%3==0&&mp[x/3]==1)
    18     {
    19         counts++;
    20         b[counts]=x/3;
    21         DFS(x/3);
    22     }
    23     if(mp[x*2]==1)
    24     {
    25         counts++;
    26         b[counts]=x*2;
    27         DFS(x*2);
    28     }
    29     return ;
    30 }
    31 int main()
    32 {
    33     int i;
    34     ll a[110];
    35     scanf("%d",&n);
    36     for(i=0;i<n;i++)
    37     {
    38         scanf("%lld",&a[i]);
    39         mp[a[i]]=1;
    40     }
    41     flag=0;
    42     for(i=0;i<n;i++)
    43     {
    44         counts=0;
    45         b[0]=a[i];
    46         DFS(a[i]);
    47         if(flag)
    48         {
    49             for(i=0;i<n;i++)
    50             {
    51                 if(i==n-1)
    52                 {
    53                     printf("%lld
    ",b[i]);
    54                 }
    55                 else
    56                 {
    57                     printf("%lld ",b[i]);
    58                 }
    59             }
    60             break;
    61         }
    62     }
    63     return 0;
    64 }

    对于这一道题其实还有另外一种方法,自定义sort的排序方式我们试分析。对于这个序列,因为3这个因数是要被除的,因此在整个序列中,3的个数必定是逐渐减少的。因此我们可以先统计所有数的3的个数,然后根据3的个数进行sort排序。而对于3的个数相同的时候,此时意味着不能进行除以3的操作,即只能进行*2的操作,因此,我们只需要将大的数排在后面即可。

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 #define LL long long int
     5 using namespace std;
     6 int my_cmp(LL a,LL b)
     7 {
     8     int count_a,count_b;
     9     count_a=0;
    10     count_b=0;
    11     while(a%3==0)
    12     {
    13         a=a/3;
    14         count_a++;
    15     }
    16     while(b%3==0)
    17     {
    18         b=b/3;
    19         count_b++;
    20     }
    21     if(count_a==count_b)
    22     {
    23         if(a<b)
    24         {
    25             return 1;
    26         }
    27         else
    28         {
    29             return 0;
    30         }
    31     }
    32     else if(count_a>count_b)
    33     {
    34         return 1;
    35     }
    36     else if(count_a<count_b)
    37     {
    38         return 0;
    39     }
    40 }
    41 int main()
    42 {
    43     LL a[111];
    44     int i,n;
    45     scanf("%d",&n);
    46     for(i=0;i<n;i++)
    47     {
    48         scanf("%lld",&a[i]);
    49     }
    50     sort(a,a+n,my_cmp);
    51     for(i=0;i<n;i++)
    52     {
    53         printf("%lld%c",a[i],i==n-1?'
    ':' ');
    54     }
    55     return 0;
    56 }
  • 相关阅读:
    UML建模之时序图(Sequence Diagram)
    UML统一建模语UML2和EnterpriseArchitect
    FTP服务器的搭建
    Ubuntu下Apache重启错误:Could not reliably determine解决
    JSP的优势 和劣势 与php的比较
    [置顶] Ajax 初步学习总结
    pv ticketlock解决虚拟环境下的spinlock问题
    Tomcat从零开始(十)Loader
    HDU 4740 The Donkey of Gui Zhou (模拟)
    关于cvScalar的那些事
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/9389310.html
Copyright © 2011-2022 走看看