zoukankan      html  css  js  c++  java
  • 1007. Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25)

    时间限制
    400 ms
    内存限制
    32000 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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 #include <iostream>
    2 #include <fstream>
    3 #include <vector>
    4 #include <string>
    5 #include <algorithm>
    6 #include <map>
    7 #include <stack>
    8 #include <cmath>
    9 #include <queue>
    10 #include <set>
    11
    12
    13 using namespace std;
    14
    15
    16
    17
    18
    19 int main()
    20 {
    21
    22
    23
    24 int N;
    25
    26 vector<int> v_n;
    27
    28 cin >> N;
    29
    30 if( N == 1 )
    31 {
    32 int n;
    33 cin >> n;
    34 cout << n << " ";
    35
    36 cout << n << " " << n << endl;
    37 return 0;
    38
    39 }
    40
    41 bool all_negative = true;
    42 for( int i = 0 ; i < N ; ++i )
    43 {
    44 int n ;
    45 cin >> n;
    46 v_n.push_back(n);
    47 if( n >= 0 )
    48 {
    49 all_negative = false;
    50 }
    51 }
    52
    53 if( all_negative )
    54 {
    55 int m = 0;
    56 for( int i = 0 ; i < v_n.size() ; ++i )
    57 {
    58 if( v_n[m] < v_n[i] )
    59 {
    60 m = i;
    61 }
    62 }
    63
    64 cout << 0 << " ";
    65
    66 cout << v_n[0] << " " << v_n[v_n.size()-1] << endl;
    67 return 0;
    68 }
    69
    70 int max = v_n[0];
    71 int start = 0;
    72 int end = 0;
    73
    74
    75 for( int i = 0 ; i < v_n.size() ; ++i )
    76 {
    77 int sum = 0;
    78 for( int j = i ; j < v_n.size() ; ++j )
    79 {
    80 sum += v_n[j];
    81
    82 if( sum > max )
    83 {
    84 start = i;
    85 end = j;
    86 max =sum;
    87 }
    88 }
    89 }
    90
    91 cout << max << " ";
    92
    93 cout << v_n[start] << " " << v_n[end] << endl;
    94
    95
    96
    97 return 0;
    98 }


  • 相关阅读:
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
    C语言I博客作业05
    C语言I博客作业04
    String详解
    数据库中索引相关基础知识
    论文笔记:RankIQA
    目标检测 | 火焰烟雾检测论文(实验部分)
    图像质量评价:合成失真图像方法
  • 原文地址:https://www.cnblogs.com/kking/p/2331809.html
Copyright © 2011-2022 走看看