Блог пользователя thequacker

Автор thequacker, история, 3 месяца назад, По-английски

Problem Link

void solve(){
    int n,x;
    cin>>n>>x;
    vector<int>c(n),h(n);
    for(int i=0;i<n;i++){
        cin>>c[i]>>h[i];
    }

    int max_h=accumulate(h.begin(),h.end(),0ll);
    vector<int>dp(max_h+1,1e9);

    dp[0]=0;
    // 0 i i i i i i 
    for(int i=0;i<n;i++){
        for(int j=max_h;j>=0;j--){
            if(j-h[i]>=0 && dp[j-h[i]]+c[i]<=i*x){
                dp[j]=min(dp[j-h[i]]+c[i],dp[j]);
            }
        }
    }
    for(int i=max_h;i>=0;i--){
        if(dp[i]<=(n-1)*x){
            cout<<i<<endl;
            break;
        }
    }
}

why is the loop being runned from max_h to 0 and not 0 to max_h?
because we clearly need dp[j-h[i]] to compute dp[j]
if possible can someone explain this problem in depth?

  • Проголосовать: нравится
  • +2
  • Проголосовать: не нравится

»
3 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Think of the dp as being of the form dp[n][h] where n is the index of the day and h is the happiness that can be bought. Now the transition is dp[i][j] = min(dp[i - 1][j - h[i]] + c[i], dp[i][j]). However, you can notice that for calculating dp[i] we only need values from dp[i - 1] and hence to save space we can do updates in-place and since $$$ j - h[i] \lt j$$$ we can loop backwards and update in the same array.

»
3 месяца назад, # |
Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

because there might be multiple wrong updates while iterating 0 to max

eg you update dp[i] with dp[i-1] then again you update dp[i+1] with the updated dp[i] instead of the orignal one, so either keep another dummy array for updates and swap dummy with dp OR make a 2D dp