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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章