P1114 "The 'Fei Chang Nan Nu' Plan"
Description
Lately, seventh-grader "XXX" has been studying how to pair up classmates (don’t overthink it—just dance partners). Through various reasoning and experiments, he has gained a lot of hands-on experience. For example, he observed that people with similar heights seem to get along better.
With Halloween approaching, "XXX" plans to organize a large-scale "Fei Chang Nan Nu" pairing event at school. He has his own unique way of selecting participants: he wants to choose an equal number of boys and girls whose heights are all very close. This is easy to achieve. He asks everyone in the school to line up in order of height, then selects a consecutive segment of people such that the number of boys and girls in that segment is equal. To make the event livelier, "XXX" naturally wants to select as many people as possible. Please write a program to tell him the maximum number of people he can select.
Input Format
The first line contains a positive integer $n\ (1\le n \le 10^5)$, representing the number of people in the school.
The second line contains $n$ space-separated numbers, each of which is $0$ or $1$. Here, $0$ represents a girl and $1$ represents a boy.
Output Format
Output a non-negative integer. This number indicates the length of the longest contiguous subarray in the input where the number of boys and girls is equal.
If no such subarray exists, output $0$.
Explanation/Hint
Translated by ChatGPT 5