Comet OJ - Contest # 11 B usiness backpack backpack treatment + + new form state complete backpack

Meaning of the questions : Do you want to calculate the maximum amount of money can get the following model:

There are  n days, the initial number when your money is  0, there are  m possible operation, the first  i  kind of make you lose the current  A i  amount of money and in  return the day after the end of n  b i  count the money. Every day can perform any number of operations, each of any number of times (after each operation, but the amount of money you can not be negative). At the end of every day you get a hold of money with the current  x-related income f ( x ), and  f ( x ) does not increase monotonically.

Analysis : First, obviously dynamic programming, and the state can be decomposed into i days ago, the amount of money currently held, final return on investment can be obtained. Selecting a smaller of the two array index, as the maximum value.

Then the state transition equation: Process day i allowance node metastasis was obtained

dp [i] [j + w [j]] = max (dp [i] [j + w [j]], dp [i-1] [j])   on the second day of such a state by the former day transfer to the state.

As for the process of storage nodes, each storing only way you can do a full backpack.

Note : This state is completely backpack is a new form, we all know, the state j knapsack problem based refer to the total capacity, and the state of j this question refers to the money on hand, that is, the remaining capacity. Simulate a realistic understanding we will know how to write the state transition equation. Note enumeration direction.

Note : not yet transferred to the inactive state, it needs to be set to a very small negative number.

#include <bits / STDC ++ H.>
 the using  namespace STD; 

const  int INF = 0x3f3f3f3f ;
 int A [ 105 ], B [ 105 ];
 int W [ 1005 ];
 int DP [ 105 ] [ 1005 + 1005 ];   // represents the state is on the first day i hand! The remaining! Money (value) may be how much investment is most 

int main () {
     int n-, m, K; 
    Scanf ( " % D% D% D " , & n-, & m, & K);
     for ( int I = 0 ; I <= k; i ++ ) {
        scanf("%d",w+i);
    }
    for(int i=1; i<=m; i++){
        scanf("%d %d",a+i,b+i);
    }

    for(int i=1; i<=n; i++){
        for(int j=0; j<=2005; j++){
            dp[i][j] = -inf;
        }
    }
    dp[1][0] = 0;

    for ( int I = 2 ; I <= n-; I ++ ) {
         for ( int J = 0 ; J <= 1000 ; J ++ ) { 
            DP [I] [J + W [J]] = max (DP [I] [ W + J [J]], DP [I- . 1 ] [J]); 
        }    
        for ( int K = . 1 ; K <= m; K ++ ) {
             for ( int J = 1000 ; J> = 0 ; J, ) { 
                DP [I] [J] = max (DP [I] [J], DP [I] [J + a [K]] + B [K]);    // J ordinary herein refers to the total capacity of the backpack , which means that the remaining capacity, that is, money on hand, so the simulation realistic understanding of what to know how to write a state transition equation
            } 
        }
    }
    int ans=-1;
    for(int i=0; i<=1000; i++){
        ans = max(ans, dp[n][i]+i+w[i]);
    }
    printf("%d\n", ans);
}

 

Guess you like

Origin www.cnblogs.com/-Zzz-/p/11588135.html