alpha_6124's blog

By alpha_6124, history, 2 hours ago, In English

getting MLE in this 1862F - Magic Will Save the World can you provide any better recursive dp approach

my code

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
using ll= long long;

typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
vvi dp;
int fun(vll &arr,ll w,ll f,ll tot,int ind,ll curr){
    if(ind==arr.size()){
        ll x=(curr+f-1)/f;
        ll y=(tot-curr+w-1)/w;
        return max(x,y);
    }
    if(dp[ind][curr]!=-1)return dp[ind][curr];
    int ans=1e8;
    ans=min(ans,fun(arr,w,f,tot,ind+1,curr+arr[ind]));
    ans=min(ans,fun(arr,w,f,tot,ind+1,curr));

    return dp[ind][curr]=ans;
}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
    int tt;
    cin >> tt;
    while (tt--) {
        ll w,f;
        cin>>w>>f;
        ll n;
        cin>>n;
        ll tot=0;
        vector<ll>arr;
        for(int i=0;i<n;i++){ll a;cin>>a;tot+=a;arr.pb(a);}
        dp=vector<vector<int>>(n+1,vi(tot+1,-1));
        ll ans=fun(arr,w,f,tot,0,0);

            cout << ans << endl;
    }
    return 0;
}
  • Vote: I like it
  • 0
  • Vote: I do not like it