• 最大子列和(附加子列初始元素和末尾元素)


    题目描述如下:

    Given a sequence of K integers ${N_1, N_2, ..., N_K}$. A continuous subsequence is defined to be ${N_i, N_{i+1}, ..., N_j}$, where $1leq ileq j leq 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 (leq 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

    c++程序如下:

     1 #include <iostream>
     2 using namespace std;
     3 void MaxSubseqSum( int a[], int N );
     4 int main()
     5 {
     6     int K;
     7     int *a;
     8     cin >> K;
     9     a = new int[K];
    10     for( int i=0; i<K; i++ )
    11         cin >> a[i];
    12     MaxSubseqSum( a, K );
    13     return 0;
    14 }
    15 void MaxSubseqSum( int a[], int N )
    16 {
    17     int MaxSum = 0;
    18     int ThisSum = 0;
    19     int Start = 0;        //初始化用来解决输入元素中既有负数,又有
    20     int End = 0;          //0元素的情况。
    21     int Count = 0;
    22     int temp = 0;
    23     for( int i=0; i<N; i++)
    24     {
    25         ThisSum += a[i];
    26         if( a[i] < 0)
    27             Count++;
    28         if( ThisSum > MaxSum )
    29         {
    30             MaxSum = ThisSum;
    31             Start = a[temp];
    32             End = a[i];
    33         }
    34         else if( ThisSum < 0 )
    35         {
    36             ThisSum = 0;
    37             temp = i+1;
    38         }
    39     }
    40     if( Count == N )  //应对输入元素全为复数的情况
    41     {
    42         Start = a[0];
    43         End = a[N-1];
    44     }
    45     cout << MaxSum << " " << Start << " " << End;
    46 }
  • 相关阅读:
    后台管理界面
    登陆页面
    Django models中关于blank与null的补充说明
    django学习之路
    Django在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'
    django2笔记:路由path语法
    四 数据库备份
    Python操作MySQL
    三 数据库其他
    Shell----简单整理
  • 原文地址:https://www.cnblogs.com/hati/p/10543312.html
Copyright © 2020-2023  润新知