zoukankan      html  css  js  c++  java
  • 连续子数组的最大和/1007. Maximum Subsequence Sum (25)

    题目描述

    HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。你会不会被他忽悠住?
     
     

    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<vector>
     2 #include<stdio.h>
     3 using namespace std;
     4 int dp[10010],s[10010]={0};
     5 int main()
     6 {
     7     int n,tem;
     8     scanf("%d",&n);
     9     vector<int> vv;
    10     bool is = 1;
    11     for(int i = 0;i < n ;++i)
    12     {
    13         scanf("%d",&tem);
    14         if(tem >= 0)
    15             is = 0;
    16         vv.push_back(tem);
    17     }
    18     if(is)
    19     {
    20         printf("0 %d %d
    ",vv[0],vv[n-1]);
    21         return 0;
    22     }
    23     dp[0] = vv[0];
    24     for(int i = 0 ;i < n ;++i)
    25     {
    26         if(dp[i-1] > 0)
    27         {
    28             dp[i] = dp[i-1] + vv[i];
    29             s[i] = s[i-1];
    30         }
    31         else
    32         {
    33             dp[i] = vv[i];
    34             s[i] = i;
    35         }
    36     }
    37     int MAX = -1;
    38     int end;
    39     for(int i = 0 ;i < n ;++i)
    40     {
    41         if(dp[i] > MAX)
    42         {
    43             MAX = dp[i];
    44             end = i;
    45         }
    46     }
    47     printf("%d %d %d
    ",MAX,vv[s[end]],vv[end]);
    48     return 0;
    49 }
  • 相关阅读:
    vue里面的v-for列表循环
    浅谈Vue.use
    js 限制输入框只能输入数字的问题
    vue computed的执行问题
    前端 html 篇
    函数声明 及 名称问题
    文件读写操作
    异常以及异常处理框架探析
    使用JDBC插入数据到ORACLE,使用标识列自增列
    session超时跃出iframe并跳到登陆页面(转载)
  • 原文地址:https://www.cnblogs.com/xiaoyesoso/p/5158462.html
Copyright © 2011-2022 走看看