01-1. Max sub-columns and questionsTime limit 10000 ms
Memory Limit 65536 KB
Code length limit 8000 B
The Standard of the Judgment procedure
Given the sequence of K-integers {N1, N2, ..., NK}, "contiguous sub columns" are defined as {Ni, ni+1, ..., Nj}, where 1 <= i <= J <= K. Maximum sub columns and is defined as the largest of all contiguous child column elements. For example, given the sequence {-2, 11,-4, 13,-5,-2}, its contiguous sub columns {11,-4, 13} have the largest and 20. You are asked to write a program that calculates the maximum number of columns for a given integer sequence.
Input Format:
The input line 1th gives a positive integer k (<= 100000), and line 2nd gives K integers, which are separated by spaces.
output Format:
Outputs the largest columns in a row. If all integers in the sequence are negative, the output is 0. Input Sample:
6
-2 11-4 13-5-2
Output Sample:
20
/*
PROBLEM: Max Sub column and Problem
solving method: Online processing
time complexity: O (n)/
#include <stdio.h>
#include <malloc.h>
int findseq (int n,int list[]);//online processing
int main ()
{
int length,i;
int *list;
int max=0;
scanf ("%d", &length);
List= (int*) calloc (length,sizeof (int));
for (i=0;i<length;i++) {
scanf ("%d", &list[i]);
Max=findseq (length,list);
printf ("%d", max);
return 0;
}
int findseq (int n,int list[]) {
int thissum,maxsum=0;
int i;
thissum=0;
for (i=0;i<n;i++) {
thissum+=list[i];
if (thissum>maxsum)
maxsum=thissum;
else if (thissum<0)
thissum=0;
}
return maxsum;
}