SDUT Max Sum

题目描述

Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.

输入

The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).

输出

For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.

示例输入

2

5 6 -1 5 4 -7

7 0 6 -1 1 -6 7 -5

示例输出

Case 1:

14 1 4



Case 2:

7 1 6
简单经典的动态规划问题
 1 #include<stdio.h>

 2 int main()

 3 {

 4     int a[10001];

 5     int n,i,j,m,k,end,begin,s,max;

 6     scanf("%d",&n);

 7     for(i=1;i<=n;i++)

 8     {

 9         k=1;

10         s=0;

11         end=begin=1;

12         max=0;

13         scanf("%d",&m);

14         for(j=1;j<=m;j++)

15         scanf("%d",&a[j]);

16         printf("Case %d:\n",i);

17         for(j=1;j<=m;j++)

18         {

19             s+=a[j];

20             if(s>max)

21             {

22                 max=s;

23                 begin=k;

24                 end=j;

25             }

26             if(s<0)

27             {

28                 s=0;k=j+1;

29             }

30         }

31         printf("%d %d %d\n\n",max,begin,end);

32     }

33 }

你可能感兴趣的:(max)