description
求最大連續子段和,並輸出此字段的起始位置和終止位置的值
input
多組用例,每組用例第一行為序列長度n,第二行n個整數表示該序列,以n=0結束輸入
output
對每個測試用例,輸出最大連續子段和及其起始位置和終止位置的值,如果序列全為負值則令最大連續子段和為0,而起止位置和終止位置輸出序列的首尾元素
sample input
6 -2 11 -4 13 -5 -2
10 -10 1 2 3 4 -5 -23 3 7 -21
6 5 -8 3 2 5 0
1 10
3 -1 -5 -2
3 -1 0 -2
0sample output
20 11 13
10 1 4
10 3 5
10 10 10
0 -1 -2
0 0 0
solution
求最大連續子段和,經典dp,用sum累加每項的值,每次累加後更新最大值,如果sum<0則令sum=0,注意記錄左右端點的值以及特判序列全為負的情況
code
#include
#define inf 1<<29
int dp[100500],n;
int main()
int ans=-inf,st,ed;
if(flag)//全為負
else
if(sum
<0)
} }
printf("%d %d %d\n",ans,dp[st],dp[ed]);
} return
0;
}
hdu 1231 最大連續子串行
狀態方程dp i max dp i 1 a i a i dp 0 a 0 include include include include include include include include include include include include include includeus...
HDU 1231 最大連續子串行
problem description 給定k個整數的序列,其任意連續子串行可表示為,其中 1 i j k。最大連續子串行是所有連續子串行中元素和最大的乙個,例如給定序列,其最大連續子串行為,最大和 為20。在今年的資料結構考卷中,要求編寫程式得到最大和,現在增加乙個要求,即還需要輸出該 子串行的第...
HDU1231 最大連續子串行
problem description 給定k個整數的序列,其任意連續子串行可表示為,其中 1 i j k。最大連續子串行是所有連續子串行中元素和最大的乙個,例如給定序列,其最大連續子串行為,最大和 為20。在今年的資料結構考卷中,要求編寫程式得到最大和,現在增加乙個要求,即還需要輸出該 子串行的第...