hdu 1003 Max Sum

Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 255288    Accepted Submission(s): 60663


Problem Description
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.
 

 

Input
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).
 

 

Output
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.
 

 

Sample Input
2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
 

 

Sample Output
Case 1: 14 1 4 Case 2: 7 1 6
 
 
题解:最简单的尺取法     一次遍历 
一路加上去   如果累加的值sum    sum小于0了    就记录开头的节点
如果sum大于了记录最大值的ans   就更新ans的值  和开头结尾
 1 //尺取法 
 2 
 3 #include<stdio.h>
 4 #include<string.h>
 5 int que[100002];
 6 main()
 7 {
 8   int t,flag;
 9   scanf("%d",&t);
10   flag=t;
11   while(t--)
12   {
13     memset(que,0,sizeof(que));
14     int n,i,temp=0;
15     int fir=1,end=1,sum=-1,max=-0x7fffffff;/*0x7fffffff这是2进制的int最大*/
16     scanf("%d",&n);
17     for(i=1;i<=n;i++)
18       scanf("%d",&que[i]);
19     for(i=1;i<=n;i++)
20     {
21       if(sum<0)
22       {
23         temp=i;
24         sum=que[i];
25         
26       }
27       else
28       {
29           sum+=que[i];
30           
31       }
32       if(sum>max)
33         {
34              max=sum;
35             fir=temp;
36             end=i; 
37         }
38          
39     }
40     
41     printf("Case %d:\n",flag-t);
42     printf("%d %d %d\n",max,fir,end);
43     if(t)
44         printf("\n");
45   }
46   return 0;
47 }

 

posted @ 2017-09-05 12:21  红雨520  阅读(113)  评论(0编辑  收藏  举报