[Luo gu oj] P1114 "very male and female" program

Source: Internet
Author: User

Valley 1114 "very men and women" program

Address: http://www.luogu.org/problem/show?pid=1114

Title Description

Recently, the first year of XXX children committed to the students in the seminar on the pairing problem (don't think too much, just a partner), through a variety of reasoning and experiments, he mastered a lot of practical experience. For example, he observes, people who are similar in height seem to be more compatible.
On the eve of Halloween, XXX prepares to plan a large "very male and female" matching event at the school. For the participants in this event, XXX has its own unique way of choosing. 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. In order to make the event more lively, xxx certainly hope that he can select the more people the better. Please write a procedure to tell him how many people he can pick up.

Input/output format

Input format:

The first line has a positive integer n, which represents the number of schools. n<=100000
The second line has n spaces separated by a number, these numbers can only be 0 or 1, where 0 is a female, 1 for a boy

Output format:

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.

Input/Output sample

Input Sample # #:

90 1 0 0 0 1 1 0 0

Sample # # of output:

6

Thinking Analysis:

(1) The simplest idea is to traverse all the substrings and then determine whether the substring satisfies the condition. (Just like looking for the maximum subsequence.) ) have n^2 substring, each substring sweep to determine whether 0, 1 occurrences of the same number of times, the complexity of O (n^3).

Further thinking will find that if a substring of length n satisfies the conditions, add that the n elements and add up must = (N/2), so that in the process of the cycle, the increment can be added, do not need to calculate each substring from the beginning, the complexity of the drop to O (n^2).

(Well, the improvement in this place is actually a similar improvement to the O (n^2) level algorithm in the maximum subsequence and problem.) )

Maximum sub-sequences and problems: http://www.cnblogs.com/huashanqingzhu/p/3861238.html

The above two algorithms to solve the maximal subsequence and problem: (The following three pictures are taken from the ppt of Zhejiang University Chen Teacher)

 
(2) To further reduce the time complexity of the algorithm, introduce the The concept of relative difference . That is, A[i] indicates the number of males in the first position-the difference between the number of girls.
is especially important to note in a[0]=0.

The above paragraph is the original topic of the website tips, thinking half a day, not too understanding.
(imagine: two The most distant and equal elements, the time complexity seems to be the square level. )
later saw someone on the network make a simple analysis of a similar topic:
http:// www.cnblogs.com/worldisimple/archive/2012/04/13/2445051.html

A 01 string that appears with 0, 1 occurrences of the oldest string of equal number

Title Description:

A string of length n is known, consisting only of 0 and 1, to find the longest substring, requiring the substring to appear equal in number of occurrences of 0 and 1.

Requires that the algorithm time complexity be as low as possible.

For example:010111000001, the bold part has 4 0, 4 x 1

The author of this paper gives a better solution to this idea:

Define a data b[n], B[i] represents the difference between the number of num_of_0-num_of_1,0 and the number of 1 from a[0...i]

Then if A[I] ~ A[j] is a sub-string that conforms to the condition, there must be b[i] = = B[j], because the middle part 0, 1 number is equal, subtraction equals 0. Only need to sweep once a[n] will be able to build out the b[n.

This problem is converted to the furthest distance of a pair of numbers, making b[i] = = B[j], because the range of b[i] must be [-n,n],-n to N the range is saved up, so every sweep to b[i], check the number on the line.

1 intA[n],b[n];2 intnum[2*n +1];3 intcount[2] = {0,0}, MaxLen =0, Currlen =0;4memset (C,2*n,-1);5  for(inti =0; i < N; ++i)6 {7count[int(A[i]) +=1;8B[i] = count[1]-count[0];9    if(num[B[i] + N] = =-1)//No, the subscript for B is poor, and the value is the subscript of a.Tennum[B[i] + N] =i; One    Else//already exist A    { -Currlen = i-num[B[i] + N] +1;//num[B[i] + N] is an existing subscript for B[i] -        if(Currlen >maxlen) theMaxLen =Currlen; -}
View Code
For the original author pseudo-code errors and not rigorous, I will not say.
It is important to look at the analysis and figure out how to use the time complexity of O (n) to complete the question of finding two farthest and equal elements in an integer sequence containing n elements .
However, the author of the article did not notice that the 0 position as the starting point for the 0 difference in statistics. Modify the code of the author of the article to get the following AC code:
1#include <stdio.h>2#include <stdlib.h>3 intMain ()4 {5     intn,t;6     int*b,*num;7     intcount[2]={0,0};//count the number of 0 and 1 in the sub-segments from the first to the I position8     intmaxlen=0, currlen=0;9     inti; Tenscanf"%d",&n); OneB= (int*)malloc((n+1)*sizeof(int)); ANum= (int*)malloc((n2+1) *sizeof(int));  -      for(i=0; i<=n;i++) -b[i]=0; the      for(i=0; i<=2*n+1; i++) -num[i]=-1; -      -      for(i=1; i<=n;i++) +     { -scanf"%d", &t);//Enter a number: 0 or 1 +count[t]++;//number of statistics 0 and 1 Ab[i]=count[0]-count[1];//B[i] Save the first I element of the 0-1 sequence: the difference between the number of 0 and 1 at     } -     if(count[0]==count[1]) printf ("%d\n", count[0]+count[1]); -     Else -     { -          for(i=0; i<=n;i++)//Note: Start scanning from 0 and 1 where the number is 0 -         { in             if(num[b[i]+n]==-1) -num[b[i]+n]=i; to             Else//already exist +             { -Currlen = I-num[b[i]+n];//Num[b[i]+n] is a b[i] existing subscript the                 if(Currlen >maxlen) *MaxLen =Currlen; $             }Panax Notoginseng         } -printf"%d\n", maxlen); the     } +      A      Free(b); the      Free(num); +     return 0; -}

[Luo gu oj] P1114 "very male and female" program

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.