Maximum Subsequence Sum-解法與各種坑

 

01-複雜度2 Maximum Subsequence Sum (25 分)

注:其實是PTA最大子列和算法的擴展題。

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.

簡單來說就是注意以下幾個點(坑):

1.如果全都是負數,則輸出【0 第一個數 最後一個數】

2.如果原序列中除了0就是負數,如:‘0 -3 -6’,則輸出【0 0 0】(這個時候sumMax肯定是0)

 

一、首先我用了最大子列和的第二種方法兩層循環,除了以上前兩個點符合以後,時間超時了...

得分:24

import java.util.Scanner;

//最大子列和第二種方法
public class Main {
//這裏笨方法,實在不知道該怎麼弄全是負數和0的情況了。
    public static int first ,last ;
    public static int finalFirst,finalLast;
    public static int maxSubseqSum1(int a[],int N) {
        int maxSum = 0;
        //i是左端,j是右端
        for (int i = 0; i < N; i++) {
            int thisSum = 0;
            //對於相同的i,不同的j,下一次直接加在上一次的結果上。
            for (int j = i; j < N; j++) {
                thisSum += a[j];
                if (thisSum > maxSum) {
                    maxSum = thisSum;
                    //記錄頭和尾
                    first = i;
                    last = j;
                }
            }
        }
         //這裏用來解決全是負數和0的情況,輸出0 0 0
        if(maxSum==0){
            finalFirst = 0;
            finalLast = 0;
        }else{
            finalFirst = a[first];
            finalLast = a[last];
        }
        return maxSum;
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int a[] = new int[N];
        for (int i = 0; i < N; i++) {
            a[i] = scanner.nextInt();
        }
             //這裏解決全是負數的情況
        int count = 0;
        for (int i = 0; i < N; i++) {
            if (a[i] < 0) count++;
        }
        if (count == N) {
            System.out.println(0+" "+a[0]+" "+a[N-1]);
        }
        else{
            System.out.println(maxSubseqSum1(a,N)+" "+finalFirst+" "+finalLast);
        }
        }
}
提交時間 狀態 分數 題目 編譯器 耗時 用戶
2019/10/19 19:30:05

部分正確

24 01-複雜度2 Java (openjdk) 142 ms 慕珩
測試點 提示 結果 耗時 內存
0 sample換1個數字。有正負,負數開頭結尾,有並列最大和 答案正確 100 ms 10876 KB
1 最大和序列中有負數 答案正確 119 ms 10892 KB
2 並列和對應相同i但是不同j,即尾是0 答案正確 142 ms 10804 KB
3 1個正數 答案正確 109 ms 10812 KB
4 全是負數 答案正確 101 ms 10684 KB
5 負數和0 答案正確 93 ms 10896 KB
6 最大和前面有一段是0 答案正確 91 ms 10716 KB
7 最大N 運行超時 -- 0 KB

二、在線處理法

後來我老老實實用了在線處理法,可還是時間超時= = ,查了很多其他同學的,也沒有結論。

import java.util.Scanner;

//最大子列和第4種方法
public class Main {
    public static int first, last;
    public static int finalFirst, finalLast;

    public static int maxSubseqSum1(int a[], int N) {
        int thisSum = 0, maxSum = 0;
        for (int i = 0; i < N; i++) {
            thisSum += a[i];
            if (thisSum > maxSum) {
                maxSum = thisSum;
                last = i;
            } else if(thisSum < 0) {
                thisSum = 0;
            }
        }
            thisSum = 0;
            for(int j = last;j>=0;j--){
                thisSum += a[j];
                if(thisSum==maxSum){
                    first = j;
                }
            }
            if (maxSum == 0) {
                finalFirst = 0;
                finalLast = 0;
            } else {
                finalFirst = a[first];
                finalLast = a[last];
            }
            return maxSum;
    }
        public static void main (String[]args){
            Scanner scanner = new Scanner(System.in);
            int N = scanner.nextInt();
            int a[] = new int[N];
            for (int i = 0; i < N; i++) {
                a[i] = scanner.nextInt();
            }
            boolean flag = true;
            for (int i = 0; i < N; i++) {
                if(a[i]>=0) flag = false;
            }
            if (flag) {
                System.out.println(0 + " " + a[0] + " " + a[N - 1]);
            } else {
                System.out.println(maxSubseqSum1(a, N) + " " + finalFirst + " " + finalLast);
            }
        }
    }

= =思路應該是對的,但是超時實在不知道如何解決了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章