zoukankan      html  css  js  c++  java
  • 最大子列和(附加子列初始元素和末尾元素)

    题目描述如下:

    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 }
  • 相关阅读:
    使用软引用构建缓存(转载)
    Android的View和ViewGroup分析(转载)
    WiFiDirect功能在Android 4.0中出现
    Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果(转载)
    android API之ActivityGroup 转载
    .9.png的制作
    android ScrollView的API详解
    JAVA的重写和重载
    关于dialog特殊设置,不销毁
    查看各国msn首页最简单的方法
  • 原文地址:https://www.cnblogs.com/hati/p/10543312.html
Copyright © 2011-2022 走看看