HDU 3506 (环形石子合并)区间dp+四边形优化
Monkey Party
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1699 Accepted Submission(s): 769
Problem Description
Far
away from our world, there is a banana forest. And many lovely monkeys
live there. One day, SDH(Song Da Hou), who is the king of banana forest,
decides to hold a big party to celebrate Crazy Bananas Day. But the
little monkeys don't know each other, so as the king, SDH must do
something.
Now there are n monkeys sitting in a circle, and each monkey has a making friends time. Also, each monkey has two neighbor. SDH wants to introduce them to each other, and the rules are:
1.every time, he can only introduce one monkey and one of this monkey's neighbor.
2.if he introduce A and B, then every monkey A already knows will know every monkey B already knows, and the total time for this introducing is the sum of the making friends time of all the monkeys A and B already knows;
3.each little monkey knows himself;
In order to begin the party and eat bananas as soon as possible, SDH want to know the mininal time he needs on introducing.
Now there are n monkeys sitting in a circle, and each monkey has a making friends time. Also, each monkey has two neighbor. SDH wants to introduce them to each other, and the rules are:
1.every time, he can only introduce one monkey and one of this monkey's neighbor.
2.if he introduce A and B, then every monkey A already knows will know every monkey B already knows, and the total time for this introducing is the sum of the making friends time of all the monkeys A and B already knows;
3.each little monkey knows himself;
In order to begin the party and eat bananas as soon as possible, SDH want to know the mininal time he needs on introducing.
Input
There
is several test cases. In each case, the first line is n(1 ≤ n ≤ 1000),
which is the number of monkeys. The next line contains n positive
integers(less than 1000), means the making friends time(in order, the
first one and the last one are neighbors). The input is end of file.
Output
For each case, you should print a line giving the mininal time SDH needs on introducing.
Sample Input
8
5 2 4 7 6 1 3 9
Sample Output
105
Author
PerfectCai
Source
首先题意说的好麻烦,英语渣表示这是smg,意思就是N只猴子围成一个小圈圈大家一开始只认识自己,猴王每次只能挑出两个人介绍他们认识,花费的时间为这两个的时间总和。定义如果介绍了A和B,那么A认识的人也将认识B认识的,这所要的时间是所有的A&&B认识的人的时间和,模拟下发现这就是一个环形石子合并问题。
对于环形我们将N的环化为2*N的直线处理,最后答案就是MIN{dp[i][i+N-1] | 1<=i<=N},N*2<=2000,传统做法会T,使用四边形优化。
对其优化内涵并不是很熟悉只知道这个定理但是看不太懂证明过程只能先记着了。
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 #define MAX 2005 5 LL dp[MAX][MAX]; 6 LL pre[MAX]={0}; 7 int s[MAX][MAX]; 8 int a[MAX]; 9 const LL inf=999999999; 10 int main() 11 { 12 int N,m,i,j,k; 13 while(scanf("%d",&N)!=EOF){m=N*2; 14 for(i=1;i<=N;++i) scanf("%d",&a[i]),a[i+N]=a[i]; 15 for(i=1;i<=m;++i) 16 { 17 pre[i]=pre[i-1]+a[i]; 18 dp[i][i]=0; 19 s[i][i]=i; 20 } 21 for(int len=2;len<=m;++len) 22 { 23 for(i=1,j=len;j<=m;i++,j++) 24 {dp[i][j]=inf; 25 for(k=s[i][j-1];k<=s[i+1][j];++k) 26 { 27 28 LL t=dp[i][k]+dp[k+1][j]+pre[j]-pre[i-1]; 29 if(dp[i][j]>t){ 30 dp[i][j]=t; 31 s[i][j]=k; 32 } 33 } 34 } 35 } 36 LL ans=inf; 37 for(i=1;i<=N;++i) 38 if(ans>dp[i][i+N-1]) ans=dp[i][i+N-1]; 39 printf("%lld\n",ans); 40 } 41 return 0; 42 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!