题解【DP100题1~10】

这事做晚了

(Dp100计划T1)

只有蓝题及以上才会水题解

分行Dp,行间没有转移

[F[L][R] = max(F[L+1][R]+2^k imes V[L],F[L][R-1]+2^k imes V[R]) ]

#include<iostream>
#include<cstdio>
#include<vector>
#include<stack>
#include<algorithm>
#include<cstring>
#define ll __int128
#define MAXN 85
ll f[MAXN][MAXN] ;
ll pow2[MAXN] ;
ll val[MAXN] ;
ll ans = 0 ; 
ll n,m ;
using namespace std ;
#define k m-(R-L)
ll dp(int L,int R){
    if(f[L][R]!=-1) return f[L][R];
    if(R-L>=1) f[L][R]=max(val[L]*pow2[k]+dp(L+1,R),dp(L,R-1)+val[R]*pow2[k]);
    else f[L][R]=val[L]*pow2[k];
    return f[L][R];
}
void put(ll v){ if(v>9) put(v/10) ; putchar(v%10+'0') ; }
void pput(ll v){ if(!v) putchar('0') ; else put(v) ; }
inline void read(ll &x) { char ch=getchar(); int f=1; ll s=0 ; while (!(ch>='0'&&ch<='9')) { if (ch=='-') f=-1; ch=getchar();} while (ch>='0'&&ch<='9') { s=(s<<3)+(s<<1)+ch-'0'; ch=getchar(); } x=s*f; }
int main(){
    read(n) , read(m) , pow2[0] = 1 ;
    for(int i=1;i<=m;++i) pow2[i] = pow2[i-1]*2 ;
    for(int nc=1;nc<=n;++nc){
        memset(f,-1,sizeof(f)) ;
        for(int i=1;i<=m;++i) read(val[i]) ;
        ans += dp(1,m) ;
    }
    pput(ans) ;
}
原文地址:https://www.cnblogs.com/tyqtyq/p/10420687.html