↓ Skip to main content
  1. Posts/

最大连续区间和的算法总结

·799 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

最大连续区间和是一个经典的问题。给定一个长度为 n 的序列 a[1],a[2]…a[n-1],a[n],求一个连续的子序列 a[i],a[i+1]…a[j-1],a[j],使得 a[i]+a[i+1]…a[j-1]+a[j]最大。

① 最简单最容易想到的就是根据定义来枚举。 枚举上下界{i,j | 0<=i<=j<=n},维护一个 max 值即可。 其中枚举上下界的时间复杂度为 O(n^2),求区间和的复杂度为 O(n),所以总时间复杂度为 O(n^3)。

1for ( i = 1 ; i <= n ; i++ )
2for ( j = i ; j <= n ; j++ )
3ans = max(ans,accumulate(a+i,a+j+1,0));

② 其实就是第一种方法的优化。 这里有个很容易想到的优化,即预处理出前缀和 sum[i]=a[0]+a[1]+…+a[i-1]+a[i],算区间和的时候即可将求区间和的复杂度降到 O(1),枚举上下界的复杂度不变,所以总时间复杂度为 O(n^2)。

1for ( i = 1 ; i <= n ; i++ )
2sum[i]=sum[i-1]+a[i];
3for ( i = 1 ; i <= n ; i++ )
4for ( j = i ; j <= n ; j++ )
5ans = max(ans,sum[j]-sum[i-1]);

③ 可以利用动态规划的思维来继续优化,得到一个线性的算法,也是最大连续区间和的标准算法 定义 maxn[i]为以 i 为结尾的最大连续和,则很容易找到递推关系:maxn[i]=max{0,maxn[i-1]}+a[i]。 所以只需要扫描一遍即可,总时间复杂度为 O(n)。

1for ( i = 1 ; i <= n ; i++ )
2{
3last = max(0,last)+a[i];
4ans = max(ans,last);
5}

④ 同样用到类似的思维。 首先也需要预处理出前缀和 sum[i],可以推出 ans=max{sum[i]-min{sum[j] } | 0<=j<i},而最小前缀和可以动态维护,所以总时间复杂度为 O(n)。

1for ( i = 1 ; i <= n ; i++ )
2    sum[i]=sum[i-1]+a[i];
3for ( i = 1 ; i <= n ; i++ )
4{
5    ans = max(ans,sum[i]-minn);
6    minn = min(minn,sum[i]);
7}

总结:虽然朴素的O(n^3)和前缀和优化的O(n^2)算法很容易想到,但代码实现却反而比方法三麻烦,第四个方法虽然有和方法三相同的复杂度,但需要一个预处理和多出的O(n)的空间,所以,方法三很好很强大。

Related

hdu 1160 FatMouse's Speed (最长上升子序列)

·769 words·2 mins
FatMouse’s Speed # Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10172 Accepted Submission(s): 4521 Special Judge Problem Description FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.

hdu 1260 tickets

·656 words·2 mins
Tickets # **Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1408 Accepted Submission(s): 687 ** Problem Description Jesus, what a great movie! Thousands of people are rushing to the cinema. However, this is really a tuff time for Joe who sells the film tickets. He is wandering when could he go back home as early as possible. A good approach, reducing the total time of tickets selling, is let adjacent people buy tickets together. As the restriction of the Ticket Seller Machine, Joe can sell a single ticket or two adjacent tickets at a time. Since you are the great JESUS, you know exactly how much time needed for every person to buy a single ticket or two tickets for him/her. Could you so kind to tell poor Joe at what time could he go back home as early as possible? If so, I guess Joe would full of appreciation for your help.

hdu 1176 免费馅饼(二维dp)

·1252 words·3 mins
免费馅饼 # **Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29065 Accepted Submission(s): 9921 ** Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼。说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的10米范围内。馅饼如果掉在了地上当然就不能吃了,所以gameboy马上卸下身上的背包去接。但由于小径两侧都不能站人,所以他只能在小径上接。由于gameboy平时老呆在房间里玩游戏,虽然在游戏中是个身手敏捷的高手,但在现实中运动神经特别迟钝,每秒种只有在移动不超过一米的范围内接住坠落的馅饼。现在给这条小径如图标上坐标:

hdu 1114 - Piggy-Bank (完全背包)

·774 words·2 mins
F - Piggy-Bank **Time Limit:**1000MS **Memory Limit:**32768KB 64bit IO Format:%I64d & %I64u Submit Status Description Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid.

hdu 1087 - Super Jumping! Jumping! Jumping! (最长上升子序列)

·536 words·2 mins
E - Super Jumping! Jumping! Jumping! **Time Limit:**1000MS **Memory Limit:**32768KB 64bit IO Format:%I64d & %I64u Submit Status Description Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path. Your task is to output the maximum value according to the given chessmen list.