順序表應用8 最大子段和之動態規劃法

2021-08-13 11:55:39 字數 1111 閱讀 7397

problem description

給定n(1<=n<=100000)個整數(可能為負數)組成的序列a[1],a[2],a[3],…,a[n],求該序列如a[i]+a[i+1]+…+a[j]的子段和的最大值。當所給的整數均為負數時定義子段和為0,依此定義,所求的最優值為: max,1<=i<=j<=n。 例如,當(a[1],a[2],a[3],a[4],a[5],a[6])=(-2,11,-4,13,-5,-2)時,最大子段和為20。

注意:本題目要求用動態規劃法求解,只需要輸出最大子段和的值。

input

第一行輸入整數n(1<=n<=100000),表示整數序列中的資料元素個數;

第二行依次輸入n個整數,對應順序表中存放的每個資料元素值。

output

輸出所求的最大子段和

example input

6

-2 11 -4 13 -5 -2

example output

#include

#include

#define list_size 100000

#define overflow -1

#define ok 1

int count;

typedef int elemtype;

typedef struct

sqlist;

int max(int a, int b)

int initlist(sqlist &l)

void creatnewlist(sqlist &l, int n)

int maxsum(sqlist &l, int n)

return max;

}int main()

鍊錶:有序鍊錶的建立

#include

#include

int k = 0, h = 0;

struct node

;struct node *creat(int n)

return head;

};void sort(struct node *head)}}

void display(struct node *head)

}int main()

3665 順序表應用8 最大子段和之動態規劃法

time limit 5ms memory limit 500kb problem description 給定n 1 n 100000 個整數 可能為負數 組成的序列a 1 a 2 a 3 a n 求該序列如a i a i 1 a j 的子段和的最大值。當所給的整數均為負數時定義子段和為0,依此定...

3665順序表應用8 最大子段和之動態規劃法

time limit 5 ms memory limit 500 kib submit statistic problem description 給定n 1 n 100000 個整數 可能為負數 組成的序列a 1 a 2 a 3 a n 求該序列如a i a i 1 a j 的子段和的最大值。當所...

sdut 順序表應用8 最大子段和之動態規劃法

順序表應用8 最大子段和之動態規劃法 time limit 5 ms memory limit 500 kib problem description 給定n 1 n 100000 個整數 可能為負數 組成的序列a 1 a 2 a 3 a n 求該序列如a i a i 1 a j 的子段和的最大值。...