4.6 a daily problem solution to a problem

triangle

Involving knowledge points:

  • Backpack dp

solution:

  • Backpack is a very classic problem dp algorithm (If you plan to learn dp students must learn knapsack problem), we can see concrete me in the group of the backpack nine stresses pdf
  • This question is about, we can do a backpack for each chest
  • And only requires a subject taken from each of a chest, the so provided DP [i] [j] for the i-th to enumerate chest, the amount of money available is equal to the number of program j
  • Then transfer equation is equal to dp [i] [z] + = dp [i-1] [z - a [i] [j]], initialization dp [0] [0] = 1
  • I i representative of a chest front, z z representative of money can be obtained, the j-th money A [i] [j] of the i-th representative of enumeration chest, DP [i] [z] is equal to the i-th front chest can be obtained z number of program money
  • J range from 0 to 10,000, from small to reach the final enumeration dp [n] [j], n represents a chest before you can get the amount of money equal to the number of program j, and k continue to make the difference to get the answer

std:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 105;
int a[maxn][maxn],dp[maxn][maxn*maxn],len[maxn];
int main()
{
    int n , k;
    cin>>n>>k;
    for(int i=1;i<=n;i++){
        cin>>len[i];
        for(int j=1;j<=len[i];j++)
            cin>>a[i][j];
    }
    dp[0][0] = 1;
    
    for(int i=1;i<=n;i++){
        for(int z=0;z<=10000;z++){
            for(int j=1;j<=len[i];j++){
                dp[i][z] += dp[i-1][z-a[i][j]];
            }
        }
    }
    ll ans = 0;
    for(int i=1;i<=10000;i++){
        if(dp[n][i] >= k){
            ans += 1ll*k*i;
            break ;
        }else{
            ans += 1ll*dp[n][i]*i;
            k -= dp[n][i];
        }
    }
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/QFNU-ACM/p/12640491.html