zoukankan      html  css  js  c++  java
  • CF#479 D:Divide by three, multiply by two(DFS)

    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 xx, writes it down on the board, and then performs with it n1n−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.

    Input

    The first line of the input contatins an integer number nn (2n1002≤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 (1ai310181≤ai≤3⋅1018) — rearranged (reordered) sequence that Polycarp can wrote down on the board.

    Output

    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.

    Examples
    input
    Copy
    6
    4 8 6 3 12 9
    output
    Copy
    9 3 6 12 4 8 
    input
    Copy
    4
    42 28 84 126
    output
    Copy
    126 42 84 28 
    input
    Copy
    2
    1000000000000000000 3000000000000000000
    output
    Copy
    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=9x=9.

    【思路】:

    有点难受,打了还掉分,心态爆炸,

    其实d题一开始的时候想歪了,以为是个小模拟,被队友带飞了。

    其实就是一个带条件的dfs

    只要从头搜索过去就对了

    标记一下。

    我做了一个dfs(一个是第几层递归,一个是上一个数是多少)

    我等他搜索到最后一层,把标记记为1

    等返回时压入一个栈再输出。

    但是我忘了压入,会压入多个,wa了好多发

    后来加个return

    就ac了

    贴上代码:

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<stack>
    using namespace std;
    typedef long long ll;
    const int MAXN=105;
    int n;
    ll math[MAXN];
    int vids[MAXN];
    stack<ll>stk;
    int flag;
    void dfs(int m,ll ans)
    {
        if(m==n+1)
        {
            //printf("%d
    ",ans);
            stk.push(ans);
            flag=1;
            return ;
        }
        if(m<=n)
        {
            for(int i=0; i<n; i++)
            {
                if(vids[i]==0&&m==1)
                {
                    vids[i]=1;
                    dfs(m+1,math[i]);
                    if(flag==1)
                        //printf("%lld
    ",ans);
                        {stk.push(ans);
                        return ;
                        }
                    vids[i]=0;
                }
                if(vids[i]==0&&m!=1)
                {
                    if(math[i]==ans*2||(ans%3==0&&math[i]==ans/3))
                    {
                        vids[i]=1;
                        dfs(m+1,math[i]);
                        if(flag==1)
                            //printf("%lld
    ",ans);
                            {stk.push(ans);
                            return ;
                            }
                        vids[i]=0;
                    }
    
                }
            }
        }
    }
    int main()
    {
        while(~scanf("%d",&n))
        {
            memset(vids,0,sizeof(vids));
            memset(math,0,sizeof(math));
            while(!stk.empty())
            {
                stk.pop();
            }
            for(int i=0; i<n; i++)
            {
                scanf("%I64d",&math[i]);
            }
            flag=0;
            dfs(1,0);
            int a=0;
            while(!stk.empty())
            {
                if(a==0)
                {
                    a++;
                    stk.pop();
                    continue;
                }
                else
                    {if(stk.size()!=1)
                        printf("%I64d ",stk.top());
                        else
                            if (stk.size()==1)
                            printf("%I64d
    ",stk.top());
                    stk.pop();
                    }
            }
        }
    }
  • 相关阅读:
    在main函数中使用django模型(附django正反向的外键关联查询)
    使用正则表达式替换文本内容
    spring 上下文和spring mvc上下文和web应用上下文servletContext之间的关系
    idea快捷键
    Spring MVC的jar包版本问题
    Spring MVC的参数类型转换
    HiddenHttpMethodFilter进行请求过滤,实现Rest风格的url
    Spring MVC的异常处理
    Spring MVC 的拦截器
    @ResponseBody&@RequestBody
  • 原文地址:https://www.cnblogs.com/qq136155330/p/9005425.html
Copyright © 2011-2022 走看看