Problem H. Secret Code-构造-贪心

  • H - Секретный шифр

  •  Gym - 101967H 
  • 题意:给你0-9个数的 每个数字的可用个数,然后从这里面挑一些构造最大的一个数,这个数满足 任意连续三位
  • 能够整除3,但是 当数字长度为1或2时是无所谓的,直接按大小输出即可,还有特判一下全为0的情况
  • #include<bits/stdc++.h>
    using namespace std;
    #define maxn 123
    string str[maxn];
    char sbhk,sblhk;
    int a[maxn],orz,cnt,b[maxn];
    void dfs(int md,int len)
    {
        int tmod;
        for(int tq=9; tq>=0; tq--)
            if((tq+md)%3==0&&a[tq]>0)
            {
                a[tq]--;
                sbhk=tq+'0';
                str[cnt]+=sbhk;
                tmod=(tq+str[cnt][len-1]-'0')%3;
                dfs(tmod,len+1);
                return;
            }
        return ;
    }
    struct node
    {
        string hk;
        int len;
        bool operator<(const node &c)const
        {
            if(len==c.len)return hk>c.hk;
            return len>c.len;
        }
    } ok[maxn];
    int main()
    {
        for(int i=0; i<10; i++)
        {
            cin>>a[i];
            b[i]=a[i];
        }
        for(int i=1; i<=9; i++)
        {
            for(int lhk=0; lhk<10; lhk++)
                a[lhk]=b[lhk];
            if(a[i]==0)continue;
            b[i]--;
            sblhk='0'+i;
            for(int j=0; j<=9; j++)
            {
                str[++cnt]+=sblhk;
                for(int lhk=0; lhk<10; lhk++)
                    a[lhk]=b[lhk];
                if(a[j]==0)continue;
                a[j]--;
                sbhk='0'+j;
                str[cnt]+=sbhk;
                orz=(i+j)%3;
                dfs(orz,2);
            }
            b[i]++;
        }
        for(int i=1; i<=cnt; i++)
        {
            ok[i].hk=str[i];
            ok[i].len=str[i].size();
        }
        if(cnt==0)cout<<0<<endl;
        else
        {
            sort(ok+1,ok+cnt+1);
            cout<<ok[1].hk<<endl;
        }
        return 0;
    }

     

  •  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值