牛客53680 「金」点石成金 (dfs)

  • 题意:给你\(n\)组数,每组4个正整数\(a,b,c,d\),每组数有两个选择:

    ​ 1.增加\(a\)个财富,消耗\(b\)点魔法.

    ​ 2.回复\(c\)点魔法,减少\(a\)个财富.

    求最后财富*魔法的最大值.

  • 题解:我们从第\(1\)组数开始dfs,我们先考虑选择第一种情况,然后不断搜索,之后在搜索第二种情况,维护一个最大值.

  • 代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <map>
    #include <set>
    #include <unordered_set>
    #include <unordered_map>
    #define ll long long
    #define fi first
    #define se second
    #define pb push_back
    #define me memset
    const int N = 1e6 + 10;
    const int mod = 1e9 + 7;
    const int INF = 0x3f3f3f3f;
    using namespace std;
    typedef pair<int,int> PII;
    typedef pair<ll,ll> PLL;
    
    int n;
    ll a[N],b[N],c[N],d[N];
    
    ll dfs(int num,ll gold,ll mana){
        if(num==n+1) return gold*mana;
        ll tmp=max((ll)0,mana-b[num]);
        ll res=dfs(num+1,gold+a[num],tmp);
        tmp=max((ll)0,gold-d[num]);
        res=max(res,dfs(num+1,tmp,mana+c[num]));
        return res;
    }
    
    int main() {
        ios::sync_with_stdio(false);cin.tie(0);
        cin>>n;
         for(int i=1;i<=n;++i) cin>>a[i]>>b[i]>>c[i]>>d[i];
    
         ll ans=dfs(1,0,0);
         printf("%lld\n",ans);
    
        return 0;
    }
    
原文地址:https://www.cnblogs.com/lr599909928/p/12973543.html