"Very men and women" plan
"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 description":
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, where0 represents a girl, 1 On behalf of a boy.
"Output description":
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 Input" |
"Sample Output" |
9 0 1 0 0 0 1 1 0 0 |
6 |
"Data range and description":
for 30% data,n<=100;
for the 50% data,n<=1;
for the 100% data,n<=100.
A little bit of thinking!
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5 using namespacestd;6 7 Const intmaxn=100005;8 intN;9 intPOS[MAXN];Ten intRPOS[MAXN]; One A intMain () - { -scanf"%d",&N); the intans=0, sum=0, a,p; -memset (pos,-1,sizeof(POS)); -memset (rpos,-1,sizeof(RpoS)); -pos[0]=0; + for(intI=1; i<=n;i++) - { +scanf"%d",&a); A if(a==1) sum++; at Elsesum--; - if(sum>=0) - { - if(pos[sum]!=-1) Ans=max (I-Pos[sum],ans); - Elsepos[sum]=i; - } in Else - { top=-sum; + if(rpos[p]!=-1) Ans=max (I-Rpos[p],ans); - Elserpos[p]=i; the } * } $printf"%d", ans);Panax Notoginseng return 0; -}
View Code
"Very men and women" plan