洛谷 P1063 能量项链 区间dp
生活随笔
收集整理的這篇文章主要介紹了
洛谷 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_queuetypedef 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?
轉載于:https://www.cnblogs.com/ckxkexing/p/9435767.html
總結
以上是生活随笔為你收集整理的洛谷 P1063 能量项链 区间dp的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: equals方法中变量在前和在后的区别
- 下一篇: java中final使用