Maximum continuous subsequence simple dp

Source: Internet
Author: User

Maximum continuous subsequence Time Limit: 2000/1000 ms (Java/Other) Memory Limit: 65536/32768 K (Java/Other) Total Submission (s): 23 Accepted Submission (s ): 9 Problem Description the sequence of K integers {N1, N2 ,..., NK}, which can be expressed as {Ni, Ni + 1 ,..., nj}, where 1 <= I <= j <= K. The maximum continuous subsequence is the element and the largest of all consecutive subsequences, such as the given sequence {-2, 11,-4, 13,-5,-2 }, its maximum continuous subsequence is {11,-4, 13}, and its maximum sum is 20. In this year's data structure examination paper, programming is required to obtain the largest sum. Now, a requirement is added, that is, the first and last elements of the subsequence must be output. The Input test Input contains several test cases. Each test case occupies two rows, Row 1 provides a positive integer K (<1st), and row 3 provides K integers, separated by spaces. When K is 0, the input ends and the case is not processed. For each test case, the Output outputs the first and last element of the largest and maximum continuous subsequences in one row, separated by spaces. If the maximum continuous subsequence is not unique, the smallest sequence numbers I and j are output (for example, 2nd and 3 groups in the input sample ). If all K elements are negative, the maximum value is 0, and the first and last elements of the entire sequence are output. Sample Input6-2 11-4 13-5-210-10 1 2 3 4-5-23 3 7-2165-8 3 2 5 01103-1-5-23-1 0- 20 Sample Output20 11 1310 1 410 3 510 10 100-1-20 0 0 [cpp] <SPAN style = "FONT-SIZE: 14px "> # include <cstdio> # include <iostream> # include <cstring> # include <cstdlib> using namespace std; int main () {int n; while (scanf ("% d", & n), n) {int flag = 1; int temp [10001], now, max; cin> temp [0]; now = max = temp [0]; int begin, end, mid; begin = end = mid = 0; for (int I = 1; I <n; I ++) cin> temp [I]; for (int j = 0; j <n; j ++) if (temp [j]> = 0) {flag = 0; break;} if (flag = 0) {for (int I = 1; I <n; I ++) {if (now + temp [I] <temp [I]) {now = temp [I]; mid = I;} else now + = temp [I]; if (max <now) {max = now; begin = mid; end = I ;}} printf ("% d \ n", max, temp [begin], temp [end]);} else printf ("0% d % d \ n", temp [0], temp [n-1]);} return 0 ;} </SPAN> # include <cstdio> # include <iostream> # include <cstring> # include <cstdlib> using namespace std; int main () {int n; while (scanf ("% d", & n), n) {int flag = 1; int temp [10001], now, max; cin> temp [0]; now = max = temp [0]; int begin, end, mid; begin = end = mid = 0; for (int I = 1; I <n; I ++) cin> temp [I]; for (int j = 0; j <n; j ++) if (temp [j]> = 0) {flag = 0; break;} if (flag = 0) {for (int I = 1; I <n; I ++) {if (now + temp [I] <temp [I]) {now = temp [I]; mid = I;} else now + = temp [I]; if (max <now) {max = now; begin = mid; end = I ;}} printf ("% d \ n", max, temp [begin], temp [end]);} else printf ("0% d % d \ n", temp [0], temp [n-1]);} return 0 ;}

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.