zoukankan      html  css  js  c++  java
  • [PAT] 1007 Maximum Subsequence Sum (25 分)Java

    1007 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N1​​, N2​​, ..., NK​​ }. A continuous subsequence is defined to be { Ni​​, Ni+1​​, ..., Nj​​ } where 1ijK. 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 import java.io.BufferedReader;
     2 import java.io.IOException;
     3 import java.io.InputStreamReader;
     4 
     5 /**
     6  * @Auther: Xingzheng Wang
     7  * @Date: 2019/2/18 15:56
     8  * @Description: pattest
     9  * @Version: 1.0
    10  */
    11 public class PAT1007 {
    12     public static void main(String[] args) throws IOException {
    13         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    14         reader.readLine();
    15         String[] split = reader.readLine().split(" ");
    16         int final_max_sum = Integer.MIN_VALUE;
    17         int sub_sum = 0, start_index = 0, final_start = 0, final_end = split.length - 1;
    18         for (int i = 0; i < split.length; i++) {
    19             int value = Integer.valueOf(split[i]);
    20             sub_sum = sub_sum + value;
    21             if (sub_sum >= 0) {
    22                 if (sub_sum > final_max_sum) {
    23                     final_max_sum = sub_sum;
    24                     if (start_index != final_start) {
    25                         final_start = start_index;
    26                     }
    27                     final_end = i;
    28                 }
    29             } else {
    30                 sub_sum = 0;
    31                 start_index = i + 1;
    32             }
    33         }
    34         if (final_max_sum < 0) {
    35             final_max_sum = 0;
    36         }
    37         System.out.print(final_max_sum + " ");
    38         System.out.print(Integer.valueOf(split[final_start]) + " ");
    39         System.out.println(Integer.valueOf(split[final_end]));
    40     }
    41 }
  • 相关阅读:
    Linux 进程学习(四) sigaction 函数
    Netty 编解码奥秘
    我的博客即将同步至 OSCHINA 社区,这是我的 OSCHINA ID:护国小将,邀请大家一同入驻:https://www.oschina.net/sharingplan/apply
    Netty数据如何在 pipeline 中流动
    PLM系统安装四:主卷服务安装(FSC缓存服务器plm4IP:42.20)
    Linux系统信息和进程相关命令(CPU,内存,进程)
    SAN交换机配置的备份还原,固件升级
    san交换机的级联
    PLM系统安装五(2):Corporate服务安装plm1IP:42.106
    第三步:服务器虚拟化XenServer实施部署文档
  • 原文地址:https://www.cnblogs.com/PureJava/p/10497952.html
Copyright © 2011-2022 走看看