01-复杂度2 Maximum Subsequence Sum(25 分)

标签: Java 数据结构
3人阅读 评论(0) 收藏 举报
分类:

 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


思路

  • 序列中有正数 → 返回:最大子列和 以及 首尾元素
  • 序列都是负数 → 返回:0 和 首尾元素
  • 序列中只有负数和0 → 返回:0 0 0

代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void onlineProcess(int[] arr) {
        int thisSum = 0, maxSum = 0;// 初始化当前和、最大和为0
        int firstNum = arr[0], lastNum = arr[arr.length-1];//初始化子序列的首尾两个数。
        boolean isFirst = true;//判断是否为最大子列的第一个数

        boolean isZeroMax = true;//判断0是不是序列里最大的数
        int cntOfNegative = 0;//统计负数数量

        for(int i = 0; i < arr.length; i++) {
            if(arr[i] > 0) {//如果序列里有正数,0就不是最大的
                isZeroMax = false;
                break;
            }
            if(arr[i] < 0) {
                cntOfNegative++;
            }
        }
        //如果只有负数和0
        if(cntOfNegative < arr.length && isZeroMax) {
            System.out.println(maxSum +" 0 0");
        }else {
            int cntOfSequence = 0;//统计子列的元素数量
            for (int i = 0; i < arr.length; i++) {

                thisSum += arr[i];// 向右累加
                cntOfSequence++;//统计累加的数量               
                if (thisSum > maxSum) {
                    maxSum = thisSum;
                    if(isFirst) {
                        firstNum = arr[i - cntOfSequence + 1];//更新最大子列的第一位数
                        isFirst = false;
                    }
                    lastNum = arr[i];//更新最大子列的最后一位数
                } else if (thisSum < 0) {//如果当前子列为负,因为不可能使后面的部分和增大,所以重新计算部分和。
                    thisSum = 0;
                    isFirst = true;
                    cntOfSequence = 0;
                }
            }
            System.out.println(maxSum +" "+ firstNum + " " + lastNum);
        }       
    }   

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        // 获取整数的数量,以及具体的数字
        int cnt = Integer.parseInt(br.readLine());
        String[] nums = br.readLine().split(" ");
        br.close();
        int[] arr = new int[cnt];
        // 将具体数字转换成int型
        for (int i = 0; i < cnt; i++) {
            arr[i] = Integer.parseInt(nums[i]);
        }
        onlineProcess(arr);
    }
}
查看评论

01-复杂度2.Maximum Subsequence Sum

/* Maximum Subsequence Sum pat1007 by zhoudaxia */ #include #include #include using namespace st...
  • whzyb1991
  • whzyb1991
  • 2015-03-05 19:17:25
  • 2365

PAT 数据结构 01-复杂度2. Maximum Subsequence Sum (25)

Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
  • tuzigg123
  • tuzigg123
  • 2015-07-13 10:29:53
  • 454

PTA 数据结构 01-复杂度2 Maximum Subsequence Sum (25分)

第七个测试点始终过不去,但是根据网上找到的答案找出了原因:若 INPUT: —10 —-2 -2 0 1 2 3 4 5 6 7 OUPUT: 28 2 7 ——-我的代码(在PAT...
  • qq_32588507
  • qq_32588507
  • 2016-05-27 07:50:59
  • 272

MOOC :01-复杂度2 Maximum Subsequence Sum

01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​, N​2​​, .....
  • lin14543
  • lin14543
  • 2015-10-07 21:55:40
  • 529

01-复杂度2 Maximum Subsequence Sum (25分) python + c++

01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of KK integers { N_1N​1​​, N_2N​2​​,...
  • EricTCartman
  • EricTCartman
  • 2017-04-06 15:43:24
  • 241

中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​...
  • fjinhao
  • fjinhao
  • 2015-09-02 21:30:25
  • 2698

PAT: 01-复杂度2. Maximum Subsequence Sum

Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
  • u014569219
  • u014569219
  • 2015-04-18 16:59:03
  • 241

算法笔记-1-最大子列和-Maximum Subsequence Sum

题目内容: Given a sequence of KK integers {N1,N2,...,NK N_1, N_2, ..., N_K}. A continuous subsequence...
  • plank_root
  • plank_root
  • 2016-09-18 22:58:39
  • 2171

Maximum Subsequence Sum[PAT1007][PTA01-复杂度2]——动态规划

Maximum Subsequence Sum 思路 实现Maximum Subsequence Sum思路数据结构视频里面简单的讲了一下动态规划的方法,但这个题还需要输出开始最后的序列数,测试点中包...
  • u013563008
  • u013563008
  • 2016-04-24 00:35:59
  • 623

数据结构 PAT 01-复杂度2 Maximum Subsequence Sum

01-复杂度2 Maximum Subsequence Sum   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K...
  • cs_hmyy
  • cs_hmyy
  • 2015-09-30 19:11:09
  • 248
    个人资料
    等级:
    访问量: 0
    积分: 142
    排名: 0