[PTA] PAT(A) 1007 Maximum Subsequence Sum (25 分)

目录

  • Problem

    • Description
    • Input
    • Output
    • Sample
      • Sample Input
      • Sample Output
  • Solution
    • Analysis
    • Code

Problem

portal: 1007 Maximum Subsequence Sum (25 分)

Description

Given a sequence of $K$ integers { $N_{1}?$, $N_{2}?$, $...$, $N_{K}$ }. A continuous subsequence is defined to be {$N_{i}$, $N_{i+1}$, $...$, $N_{j}$} where $1 \leq i \leq j \leq 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

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer $K (\leq 10000)$. The second line contains $K$ numbers, separated by a space.

Output

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

Sample Input

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output

10 1 4

Solution

Analysis

题目要求计算给出的一个序列中最大的连续子序列和。
一个个累加,如果“临时最大和”大于“最大和”,那么更新“最大和”的信息,包括首位置和末位置的信息;如果“临时最大和”小于0的话,那么将临时首位置标记修改为当前位置,然后重新将“临时最大和”赋值为0,因为如果小于0的话,在以后的序列中,只会减少序列的和。
语言组织太乱,建议参考别人的 :柳婼 の blog

Code

#include <bits/stdc++.h>
using namespace std;

int main(void) {
    int K, first_number, second_number, temp_first_number;
    long max_sum = -1, temp_max_sum = -1;

    cin >> K;
    int value[K];
    for (int i = 0; i < K; i++) {
        cin >> value[i];
        if (temp_max_sum < 0) {
            temp_first_number = value[i];
            temp_max_sum = 0;
        }
        temp_max_sum += value[i];
        if (temp_max_sum > max_sum) {
            max_sum = temp_max_sum;
            first_number = temp_first_number;
            second_number = value[i];
        }
    }
    if (max_sum == -1) {
        cout << "0 " << value[0] << " " << value[K - 1] << endl;
    } else {
        cout << max_sum << " " << first_number << " " << second_number << endl;
    }
}

原文地址:https://www.cnblogs.com/by-sknight/p/11441308.html

时间: 2024-08-04 22:21:19

[PTA] PAT(A) 1007 Maximum Subsequence Sum (25 分)的相关文章

1007 Maximum Subsequence Sum (25分) 求最大连续区间和

1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N?1??, N?2??, ..., N?K?? }. A continuous subsequence is defined to be { N?i??, N?i+1??, ..., N?j?? } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has th

pta 1007 Maximum Subsequence Sum (25分)

Given a sequence of K integers { N?1??, N?2??, ..., N?K?? }. A continuous subsequence is defined to be { N?i??, N?i+1??, ..., N?j?? } where 1. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For exampl

数据结构课后练习题(练习一)1007 Maximum Subsequence Sum (25 分)

Given a sequence of K integers { N?1??, N?2??, ..., N?K?? }. A continuous subsequence is defined to be { N?i??, N?i+1??, ..., N?j?? } where 1. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For exampl

1007 Maximum Subsequence Sum (25 分)

Given a sequence of K integers { N?1??, N?2??, ..., N?K?? }. A continuous subsequence is defined to be { N?i??, N?i+1??, ..., N?j?? } where 1. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For exampl

PTA 1007 Maximum Subsequence Sum (25 分)

1 #include <stdio.h> 2 #include <iostream> 3 #include <string.h> 4 #include <vector> 5 #include <algorithm> 6 #include <cassert> 7 #include <queue> 8 using namespace std; 9 int n; 10 int main() 11 { 12 cin >>

1007 Maximum Subsequence Sum (25分)(动态规划DP)

#include <vector> #include<iostream> using namespace std; int main() { int k; cin>>k; int left_index=0,right_index=k-1,sum=-1,tmp=0,tmp_index=0; vector <int> num(k); for(int i=0;i<k;i++) { cin>>num[i]; tmp+=num[i]; if(tmp&

1007. Maximum Subsequence Sum (25)——PAT (Advanced Level) Practise

题目信息: 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 <=

1007 Maximum Subsequence Sum (25)(25 分)

1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A continuous subsequence is defined to be { N~i~, N~i+1~, ..., N~j~ } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence

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

1??, N2N_2N?2??, ..., NKN_KN?K?? }. A continuous subsequence is defined to be { NiN_iN?i??, Ni+1N_{i+1}N?i+1??, ..., NjN_jN?j?? } where 1≤i≤j≤K1 \le i \le j \le K1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum