zoukankan      html  css  js  c++  java
  • PTA 5-1 Maximum Subsequence Sum (25)

    题目:

    PAT: http://www.patest.cn/contests/pat-a-practise/1007
    PTA: http://pta.patest.cn/pta/test/18/exam/4/question/611

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

    Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

    Input Specification:

    Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.

    Output Specification:

    For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

    Sample Input:

    10
    -10 1 2 3 4 -5 -23 3 7 -21
    

    Sample Output:

    10 1 4

    输出出最大子列和及其首尾元素。

    注意:
    1、当数据全为负时,最大子列和为0,输出整个数列的第一个和最后一个元素。
    2、当数据只有0和负数时,最大子列和也为0,但是这时不能输出整个数列的首元素和尾元素,因为输入数据并不全为负,应该输出子列的首尾元素。

    例如:
    3
    -2 0 –3
    输出结果应该为:
    0 0 0

    处理时,需要两个数first, last分别记录最大子列和的首元素和尾元素位置(最终)。
    tempfirst, templast可看作指针,可节省一层循环。时间复杂度:O(n)。

    最后计算出的最大子列和有两种情况:
    - 结果为正数时,输出子列的首尾元素;
    - 结果为0时,需要进一步判断输入数据是全为负数还是有存在0元素的情况,这设置了一个变量flag用于标记这含有元素0的情况。具体代码如下

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int k;
        cin >> k;
        int a[k];
        for(int i=0; i<k; i++)
            cin >> a[i];
    
        int flat=0; //标记是否有0元素
        int first,last;
        int tempfirst,templast;
        int thissum,maxsum;
    
        first=last=-1;
        tempfirst=templast=0;
        thissum=maxsum=0;
    
        for(int j=0; j<k; j++){
            if(a[j]>0){
                thissum+=a[j];
                templast=j;
                if(thissum>maxsum){
                    maxsum=thissum;
                    first=tempfirst;    //只有此时更新最终的 first 和 last
                    last=templast;
                }
            }
            else{
                if(a[j]==0)
                    flat=1;
                thissum+=a[j];
                if(thissum<=0){
                    thissum=0;
                    tempfirst=j+1;
                    templast=tempfirst;
                }
                else
                    templast++; //遇到负数templast先继续往后移
            }
        }
        if(first==-1 && flat==0)    //当数据全为负时,最大子列和为0,输出整个数列的第一个和最后一个元素
            cout << "0 " << a[0] << " " << a[k-1];
        else if(first==-1 && flat==1)   //当数据只有0和负数时,最大子列和为0
            cout << "0 0 0";
        else
            cout << maxsum << " " << a[first] << " " << a[last];
        return 0;
    }
    
  • 相关阅读:
    model.object对象查询过滤、增删改、Q
    模板中的标签、过滤器
    模板(template)包含与继承
    url用法
    AD用户登录验证,遍历OU(LDAP)
    Python下操作sqlite3
    多线程应用-类(thread)
    数组(list)分组、分段
    多线程应用-函数方式(thread)
    IntelliJ IDEA maven项目 ***
  • 原文地址:https://www.cnblogs.com/claremore/p/4784032.html
Copyright © 2011-2022 走看看