洛谷 P1063 能量项链 区间dp

洛谷 P1063 

题意:

在一串项链中,是环状的,第 $i$ 颗珠子有两个能量$a[i]$和$a[i+1]$,第i+1颗珠子有两个能量$a[i+1]$和$a[i+2]$,可以合并两个珠子,得到$a[i]*a[i+1]*a[i+2]$的能量,这两个珠子合并成a[i]和a[i+2]的新珠子,问通过合理的操作,能得到的最大的能量。

思路:

区间dp,首先环状的变成链状的,要把区间翻倍复制,枚举左右端点和每个区间的分界点,得到结果。注意,要先从小到大枚举右端点。

当然也可以开用最外层枚举区间长度得方法。

 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <cstdlib>
#include <iterator>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <cctype>
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const double PI=acos(-1.0);

template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}
// #define _DEBUG;         //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------------show time----------------------*/

            const int maxn = 109;
            int n;
            int a[maxn*2];
            int dp[maxn*2][maxn*2];
int main(){    
            OKC;
            cin>>n;
            for(int i=1; i<=n; i++){
                cin>>a[i];
                a[i+n] = a[i];
            }
            int ans = 0;
            for(int ri=1; ri<=2*n-1; ri++){
                for(int le=ri-1; ri-le+1 <=n&&le>=1; le--){
                    for(int k=le; k<ri; k++){
                        dp[le][ri] = max(dp[le][ri] , dp[le][k] + dp[k+1][ri] + a[le]*a[k+1]*a[ri+1]);

                        ans = max(ans, dp[le][ri]);
                    }
                }
            }
            cout<<ans<<endl;
            return 0;
}
洛谷 P1063

 

posted @ 2018-08-07 11:02  ckxkexing  阅读(123)  评论(0编辑  收藏  举报