1170: Very male and female (xcoj prefix and)

Source: Internet
Author: User

1170: Very male and female time limit: 1 Sec memory limit: MB
Submitted: 9 Resolution:
Label submission Statistics Discussion edition

Title Description

MATRIX67 has been a "matchmaker" for many times. So he gained a lot of experience. For example, people who are similar in height seem to be more compatible with Matrix67.
Matrix67 in the school to plan a large "very male and female" matching activities. For the participants of this event, MATRIX67 has its own unique choice. He wants to be able to choose people with equal numbers of men and women who are close to each other. This choice is simple to implement. He had all the people in the school lined up in a row, and then elected a succession of individuals, which made the men and women equal in number. Matrix67 certainly hoped that the more people he could elect, the better. Please write a procedure to tell him how many people he can pick up.

Input

The first line has a positive integer n, which represents the number of schools.
The second line has n spaces separated by a number that can only be 0 or 1, where 0 represents a girl and 1 represents a boy.

For 30% of data,n<=100;
For 50% of data, N<=1 000;
For 100% of data, N<=100 000.

Output

Outputs a non-negative integer. This number represents the longest number of sub-sequence lengths in the input data that are equal to the numbers of males and females.
If there are no sub-sequences with equal numbers of men and women, output 0.

Sample input90 1 0 0 0 1 1 0 0Sample output6with prefixes and calculations, this sequence satisfies the requirements when the (Ans[j]-ans[i-1]) *2== (j-i+1) is satisfied. It is important to note that the inner layer traverses the j=i+sum+1 instead of the j=i+1, removing the scope that is not needed for discussion, or it will time out
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <algorithm>5 using namespacestd;6 BOOLa[100005];7 intans[100005];8 intMain ()9 {Ten     inti,j; One     intN; AFreopen ("In.txt","R", stdin); -      while(SCANF ("%d", &n)! =EOF) -     { the         intsum=0; -ans[0]=0; -          for(i=1; i<=n;i++) -         { +scanf"%d",&a[i]); -ans[i]=ans[i-1]+A[i]; +         } A          for(i=1; i<=n;i++) at         { -              for(j=i+sum+1; j<=n;j+=2) -             { -                 if((ans[j]-ans[i-1])*2= = (j-i+1)) -Sum=max (j-i+1, sum); -             } in         } -printf"%d\n", sum); to     } +     return 0; -}

1170: Very male and female (xcoj prefix and)

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.