An algorithm question-symbol balancing

Source: Internet
Author: User

A sequence composed of N integers A1, A2, and. The symbol of element a [I] in the sequence is defined:
When a [I]> 0, SGN (A [I]) = 1; When a [I] = 0, SGN (A [I]) = 0; when a [I] <0, SGN (A [I]) =-1;
The symbol balancing problem requires the length of the longest symbol balancing segment of the given sequence L, that is:
L = max {J-I + 1}, where 1 = <I <= j <= n, and it meets SGN (A [I]) + SGN (A [I + 1]) +... + SGN (A [J]) = 0 indicates the maximum value of all I in the expression J.
For example, when n = 10, the corresponding sequence is: 1, 1,-1,-2, 0, 1,-1,-1, L = 9.
Algorithm Design:
Given a sequence composed of N integers A1, A2, and an, design an O (n) time algorithm to calculate its longest symbol.
The length of the balancing segment.
Data input:
Input data is provided by the file input.txt. The first row of the file is a positive integer N, and the second row is an integer sequence A1, A2,
Result output:
Output the length of the calculated longest sign balancing section to output.txt.
Input File example output file example
Input.txt output.txt
10 9
1 1-1-2 0 1 3-1 2-1

 

# Include <stdio. h>
# Include <assert. h>

# Define sign (x) = 0? 0: (x)> 0? 1:-1 ))

Int calc (INT data [], int CNT)
{
If (CNT <= 0)
Return 0;

Int * value = new int [CNT + 1];
Int * First = new int [CNT + 1];
Int * Last = new int [CNT + 1];

For (INT I = 0; I <CNT + 1; I ++)
First [I] =-1;

/*
Initialize value, then value [I] represents
Sum of the first ith elements 'sign value.
*/
Value [0] = 0;
For (INT I = 1; I <CNT + 1; I ++)
Value [I] = value [I-1] + sign (data [I-1]);

/*
For each element V in value [0... CNT], first [v]
Denotes the index of V's first appearance, and
Last [v] denotes the last appearance.
*/
For (INT I = 0; I <CNT + 1; I ++)
{
Int v = value [I];
If (first [v] =-1)
First [v] = last [v] = I;
Else
Last [v] = I;
}

Int max = 0;
For (INT I = 0; I <CNT + 1; I ++)
If (first [I]! =-1 & max <last [I]-first [I])
Max = last [I]-first [I];

Delete value;
Delete first;
Delete last;

Return Max;
}

Int main ()
{
Int data1 [] = {1, 1,-1,-2, 0, 1, 3,-1, 2,-1 };
Int data2 [] = {1 };
Int data3 [] = {0 };

Int max1 = calc (data1, sizeof (data1)/sizeof (data1 [0]);
Int max2 = calc (data2, sizeof (data2)/sizeof (data2 [0]);
Int max3 = calc (data3, sizeof (data3)/sizeof (data3 [0]);

Assert (max1 = 9 );
Assert (max2 = 0 );
Assert (max3 = 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.