PID5 能量项链 矩阵链乘

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ahjkl007/article/details/39136421

题意:很简单的矩阵链乘

解法:记忆话搜索

#include<cstdio>
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
#define LL long long
#define maxn 222
int n;
LL d[maxn][maxn],ll[maxn],rr[maxn];
LL dfs(int l,int r){
    if(l==r)return (LL)0;
    if(d[l][r])return d[l][r];
    if(r-l==1){
        d[l][r]=ll[l]*rr[l]*rr[r]; return d[l][r];
    }
    LL ans=0;
    
    for(int i=l;i<r;++i){
        ans=max(ans,dfs(l,i)+dfs(i+1,r)+ll[l]*ll[i+1]*rr[r]);
    }
//    printf("%d %d %lld\n",l,r,ans);
    d[l][r]=ans;
    return ans;
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        scanf("%lld",&ll[i]);
        rr[i-1]=ll[i];
    }
    for(int i=n+1;i<=2*n;++i){
        ll[i]=ll[i-n];
        rr[i-1]=ll[i];
    }
//    for(int i=1;i<=2*n;++i)printf("%lld %lld\n",ll[i],rr[i]);
    
    for(int i=1;i<=n;++i){
        dfs(i,i+n-1);
    }
    LL mi=0;
    for(int i=1;i<=n;++i){
        mi=max(mi,d[i][i+n-1]);
    }
    printf("%lld\n",mi);
    return 0;
}


展开阅读全文

没有更多推荐了,返回首页