Hangdian 1003 Max sum TLE

Source: Internet
Author: User

This question requires the largest sum of consecutive subsequences. So when I see the question, I think of listing all the situations and comparing the two, taking the maximum value (that is, the meaning of updating the maximum value), this idea is very simple, but it will time out, the time complexity is O (n ^ 3), because there are three for statements

# Include <stdio. h> # define maxn 101000int main () {int ncase, flag = 1, n, Max, sum = 0, H, Z, a [maxn]; long I, j, k; scanf ("% d", & ncase); While (flag <= ncase) {max =-1001; scanf ("% d", & N ); for (I = 0; I <n; I ++) {scanf ("% d", & A [I]) ;}for (I = 0; I <N; I ++) {for (j = I; j <n-1-i; j ++) {sum = 0; For (k = I; k <= J; k ++) sum + = A [k]; If (sum> MAX) {max = sum; H = I; Z = J ;}}}} printf ("case % d \ n", flag); printf ("% d % LD \ n", Max, H + 1, Z + 1 ); printf ("\ n"); flag ++ ;}}

Hangdian 1003 Max sum TLE

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.