HDU-1003-Max Sum



Max Sum

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


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
 

Author
Ignatius.L





http://acm.hdu.edu.cn/showproblem.php?pid=1231(同一类型)

刘文强刘老师曾经在算法分析课上讲过一系列的子序列问题,最大最小最特殊一箩筐一箩筐的。

思路在于:
一边寻找前X位最小子序列的X的值(其中第0项为0),一边对比查找最大子序列。
每次更新最小子序列的X的值时(为负值,否则一直不会更新,因为第0项为0),重新设置X+1为新序列的左侧第一位序号以便查找后面是否会有更大的子序列。
即为:
if(sum[j - 1] < min) 
{
   min = sum[j - 1];       //找到前j - 1项最小之合,极有可能为负值。 
   l = j;                  //左侧最小子序列排除,重新记录左端序号。 
}
if(sum[j] - min > best)
{
   best = sum[j] - min;    //最大子序列之合 
   left=l;                 //若全部为正则记录为1,否则记录排除左侧最小子序列 
   right=j;
}

其他:用到了memset函数(memset - 搜狗百科 http://baike.sogou.com/v87712.htm)
void *memset(void *s, int ch, size_t n);

起功能是将s所指向的某一块内存中的每个字节的内容全部设置为ch指定的ASCII值,块的大小由第三个参数指定,这个函数通常为新申请的内存做初始化工作,其返回值为指向S的指针。memset的作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法。

需要的头文件:在C中是<string.h>,在C++中是<cstring>。




下面为可提交通过代码:
#include<stdio.h>
#include<string.h>
int main()
{
    int t, n, i, j, a, k, l;
    int sum[100000 + 10];
    int left, right, min, best;
    scanf("%d", &t);
    for(k = 1; k <= t; k++)
    {
        scanf("%d", &n);
        
        memset(sum, 0, sizeof(sum)); //0处开始置为0 
        
        for(i = 1; i <= n; i++)
  {
      scanf("%d", &a);         //1处开始录入 
   sum[i] = sum[i - 1] + a; //前i项合 
  }
        left = -1;
  right = -1;
        min = 100000001;
  best = -100000001;
        for(j = 1; j <= n; j++)     //以j结尾的子串
        {
            if(sum[j - 1] < min) 
            {
   min = sum[j - 1];       //找到前j - 1项最小之合,极有可能为负值。 
      l = j;                  //左侧最小子序列排除,重新记录左端序号。 
            }
            if(sum[j] - min > best)
            {
                best = sum[j] - min;//最大子序列之合 
                left=l;//若全部为正则记录为1,否则记录排除左侧最小子序列 
                right=j;
            }
        }
        
        if (k == t) printf("Case %d:\n%d %d %d\n", k, best, left, right);
        else      printf("Case %d:\n%d %d %d\n\n", k, best, left, right);
        //两边区别在于是否为最后一次测试数据。 
    }
    
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章