每日一题 - 240408 - 最大数字


  • TAG - 芝士水题、算法 − 【算法 − D F S 、贪心】 芝士水题、算法 - 【算法 - DFS、贪心】 芝士水题、算法【算法DFS、贪心】
  • 时间复杂度 - O ( ∗ ) O(\ast) O()

//
#include <bits/stdc++.h>
using namespace std;
#define int long long 

string s;
int cntA, cntB, ans;

void dfs(int idx, int sum) {
    if (idx >= s.size()) {
        ans = max(ans, sum);
        return ;
    }

    int tt = s[idx] - '0';
    int needA = min(9 - tt, cntA);
    int needB = tt + 1;

    if (cntA >= needA) {
        cntA -= needA;
        dfs(idx + 1, sum * 10 + tt + needA);
        cntA += needA;
    }
    if (cntB >= needB) {
        cntB -= needB;
        dfs(idx + 1, sum * 10 + 9);
        cntB += needB;
    }
    // dfs(idx + 1, sum * 10 + tt);     // 要用就用在位置靠前的(贪心)
}

void solve() {
    cin >> s >> cntA >> cntB;
    dfs(0, 0);
    cout << ans << endl;
}

signed main() {
    int t = 1;
    // scanf("%lld", &t);
    while (t--) solve();
    return 0;
}

实现细节

  • `

参考示意图

  • `

参考链接

  • `

作者 | 乐意奥AI

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值