1007. Maximum subsequence sum (25) -- Pat (Advanced Level) practise

Source: Internet
Author: User

Question information:

1007. Maximum subsequence sum (25) Time Limit 400 MS
The memory limit is 32000 kb.
Code length limit: 16000 B
Criterion author 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 <= K.Maximum subsequenceIs 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 last numbers of the whole sequence.

Sample input:
10-10 1 2 3 4 -5 -23 3 7 -21
Sample output:
10 1 4


The Code is as follows:


#include <iostream>#include <vector>using namespace std;int main(){int n, t, s = 0, e, mx = 0, tmx = 0, ts, te; cin >> n;vector<int> vec;bool flag = 0;for (int i = 0; i < n; i++){cin >> t;if (t>=0) flag = 1;if (mx >= 0){mx += t;e = i;}else{mx = t;s = e = i;}if (mx > tmx || (mx == 0 && tmx == 0)){tmx = mx; ts = s;te = e;}vec.push_back(t);}if (flag){cout << tmx << " " << vec[ts] << " " << vec[te] << endl;}elsecout << "0 " << vec[0] << " " << vec[vec.size() - 1] << endl;return 0;}


1007. Maximum subsequence sum (25) -- Pat (Advanced Level) practise

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.