【JZOJ5263】分手是祝愿

Description

Pic

请注意本题的数据范围。

Input

Output

Sample Input

2
2
15 19
3
30 40 20

Sample Output

285
2600

Hint

数据范围:
30% n<=9
100% n,a<=100, T<=5

Source

动态规划 /DFS /分治

 

题解:

  区间DP裸题,注意a可以等于100.

 

代码:

  

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#define MAXN 1000
#define ll long long
#define mod 100
using namespace std;
ll a[MAXN],b[MAXN][MAXN],dp[MAXN][MAXN],num[MAXN][MAXN];
int n;

ll getit(int x,int y){
    ll ans=0;
    if(x>y) swap(x,y);
    if(y-x==0) return a[x];
    for(int i=x;i<=y;i++) ans=ans+a[i],ans%=mod;
    return ans;
}

void pre(){
    memset(num,0,sizeof(num));
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
            num[i][j]=num[j][i]=getit(i,j);
        }
}

ll DP(int l,int r){
    if(r-l+1==1) return 0;
    if(l>r) return 1ll<<50;
    if(b[l][r]) return dp[l][r];
    b[l][r]=1;
    for(int k=l;k<=r;k++){
        ll ret=1ll<<60;
        ret=DP(l,k)+DP(k+1,r)+num[l][k]*num[k+1][r];
        dp[l][r]=min(dp[l][r],ret);
    }
    return dp[l][r];
}

void work(){
    cin>>n;
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    pre();
    memset(dp,37,sizeof(dp));
    memset(b,0,sizeof(b));
    printf("%lld\n",DP(1,n));
}

int main()
{
    int t;cin>>t;
    while(t--){
        work();
    }
    return 0;
}

 

posted @ 2018-09-16 21:40  人间失格—太宰治  阅读(166)  评论(0编辑  收藏  举报