codeforces977D(显而易见的dfs)

27 篇文章 0 订阅
4 篇文章 0 订阅

AC代码:

#include<bits/stdc++.h>
using namespace std;
long long a[107];
map<long long,int> mp;
stack<long long> s;
int check1(long long x){
    if(mp[x*3]) return 1;
    else return 0;
}
int check2(long long x){
    if (x%2==0 && mp[x/2]) return 1;
    else return 0;
}
void dfs(long long now,int left){
    if(left==0){
            s.push(now);
        while(!s.empty()){
            long long a=s.top();
            cout<<a<<" ";
            s.pop();
        }
    }
    if(check1(now)==1){
        s.push(now);
        mp[now*3]--;
      //  cout<<"check 1 "<<now*3<<" "<<left-1<<endl;
        dfs(now*3,left-1);
      //  cout<<"not "<<now*3<<" "<<left-1<<endl;
        mp[now*3]++;
        s.pop();
    }
    if(check2(now)==1){
        s.push(now);
        mp[now/2]--;
      //  cout<<"check 2 "<<now/2<<" "<<left-1<<endl;
        dfs(now/2,left-1);
      //  cout<<"not "<<now/2<<" "<<left-1<<endl;
        mp[now/2]++;
        s.pop();
    }
    return;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        mp[a[i]]++;
    }
    long long beg;
    for(int i=1;i<=n;i++){
        if((a[i]%3!=0 || !mp[a[i]/3]) && !mp[a[i]*2]){
            beg=a[i];
            break;
        }
    }
  //  cout<<beg<<endl;
    dfs(beg,n-1);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值