当前位置:网站首页>320. Energy Necklace (ring, interval DP)
320. Energy Necklace (ring, interval DP)
2022-07-03 22:08:00 【seez】
320. Energy necklace - AcWing Question bank
analysis : It can be regarded as a circular interval dp problem
Altogether 4 A bead , There are four marks as shown in the figure , It can be merged to the end, leaving only 2 A sign , That is to say 1 A bead .
Then at least it needs 3 Tags can continue to merge , If it is less than three tags, there is no need to merge .
State means : All consolidation i~j A collection of tags
State calculation / Basis of division : according to i~j The marks in the middle can be divided into those that are neither heavy nor missing j-i-1 A collection of , because i,j It means the mark , Therefore, you cannot participate in the merger (dp[i][i] Not a bead , Do not participate in the operation , therefore dp[i][j],j>=i+1)
The recurrence formula is dp[i][k]+dp[k][j], Because the set before the breakpoint k As a tail mark , The latter set k As a header mark
- i+1 As a breakpoint , Merge dp[i][i+1],dp[i+1][j],dp[i][j]=dp[i][i+1]+dp[i+1][j]+w[i]*w[i+1]*w[j]
- ...
- j-1 As a breakpoint , Merge dp[i][j-1],dp[j-1][j],dp[i][j]=dp[i][j-1]+dp[j-1][j]+w[i]*w[j-1]*w[j]
Initialization and boundary are 0
Recursive method :
for(int len=1;len<=n+1;len++)// Because the right endpoint may be the starting point , therefore <=n+1
// Because the tail mark of the last bead is the first mark
for(int i=1;i+len-1<=2*n;i++)
{
...
}
ac Code
#include <iostream>
#include <algorithm>
using namespace std;
const int N=205;
const int INF=0x3f3f3f3f;
int a[N];
int s[N];
int dp[N][N];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i],a[n+i]=a[i];
for(int len=1;len<=n+1;len++)
for(int i=1;i+len-1<=2*n;i++)
{
int j=i+len-1;
for(int k=i+1;k<j;k++)
dp[i][j]=max(dp[i][j],dp[i][k]+dp[k][j]+a[i]*a[k]*a[j]);
}
int res=0;
for(int i=1;i<=n;i++)
res=max(res,dp[i][i+n]);
cout<<res;
}
边栏推荐
- Capturing and sorting out external articles -- autoresponder, composer, statistics [III]
- [dynamic programming] Ji Suan Ke: Suan tou Jun breaks through the barrier (variant of the longest increasing subsequence)
- Decompile and modify the non source exe or DLL with dnspy
- Kali2021.4a build PWN environment
- Rest reference
- Global and Chinese market of gallic acid 2022-2028: Research Report on technology, participants, trends, market size and share
- 内存分析器 (MAT)
- gslb(global server load balance)技術的一點理解
- Go language slice interview real question 7 consecutive questions
- How to obtain opensea data through opensea JS
猜你喜欢
使用dnSpy對無源碼EXE或DLL進行反編譯並且修改
2022 G3 boiler water treatment registration examination and G3 boiler water treatment examination papers
Blue Bridge Cup Guoxin Changtian MCU -- program download (III)
Awk getting started to proficient series - awk quick start
Teach you how to install aidlux (1 installation)
Why use pycharm to run the use case successfully but cannot exit?
Intimacy communication -- [repair relationship] - use communication to heal injuries
gslb(global server load balance)技術的一點理解
On my first day at work, this API timeout optimization put me down!
Minio deployment
随机推荐
Station B, dark horse programmer, employee management system, access conflict related (there is an unhandled exception at 0x00007ff633a4c54d (in employee management system.Exe): 0xc0000005: read locat
Report on the development status and investment planning trends of China's data center industry Ⓡ 2022 ~ 2028
仿网易云音乐小程序
Morning flowers and evening flowers
Great gods, I want to send two broadcast streams: 1. Load basic data from MySQL and 2. Load changes in basic data from Kafka
Luogu deep foundation part 1 Introduction to language Chapter 6 string and file operation
Tkinter Huarong Road 4x4 tutorial III
Asynchronous artifact: implementation principle and usage scenario of completable future
Data consistency between redis and database
Tidb's initial experience of ticdc6.0
JS Demo calcule combien de jours il reste de l'année
regular expression
WiFi 2.4g/5g/6g channel distribution
常用sql集合
The 14th five year plan and investment feasibility study report of China's industry university research cooperation Ⓧ 2022 ~ 2028
Let me ask you a question. Have you ever used the asynchronous io of Flink SQL to associate dimension tables in MySQL? I set various settings according to the official website
Data consistency between redis and database
Base ring tree Cartesian tree
鹏城杯 WEB_WP
内存分析器 (MAT)