Luo Gu problem solution --P1114: "very men and women" program

Related topics

Topic Link

Luo Valley, https://www.luogu.com.cn/problem/P1114 .

Total passenger garlic, https://nanti.jisuanke.com/t/T1853 .

My OJ, http://47.110.135.197/problem.php?id=5252 .

Title Description

Recently, the beginning of a year of XXXchildren working on matching problem (think too much, just dance partner) classmates through various experiments and reasoning, he mastered a large number of actual combat experience. For example, According to his observation, similar height who seems to be more chummy.

Halloween is approaching, XXXready to plan a large-scale "very men and women" matching activities in schools. For the participants of the event, XXXthey have their own unique method of choice. He hopes to select an equal number of men and women and some people are tall and close. To implement this choice is very simple. He let all schools according to the height of a row, and then choose from several consecutive individual, so that these people equal numbers of men and women. To make the event more fun, XXXof course, hope that he will elect many people as possible. Please write a program to tell him that he can choose the maximum number of people.

Input Format

The first line there is a positive integer n, the number of representatives of the school. n ≤ 100000

The second row has n numbers separated by a space, these numbers can only be 0 or 1, wherein 0 represents a girl, represents a boy.

Output Format

Output a non-negative integer. This number indicates the longest in the input data sequence of length equal to the period of males and females.

If the sequence equal number of men and women does not exist, output 0.

SAMPLE INPUT

9
0 1 0 0 0 1 1 0 0

Sample Output

6

Topic analysis

Analysis of the meaning of problems

Find the length n number of columns, the longest sequence [l, r], which sequence comprises the same number of 1 and 0. what is the meaning of this? We think about. Array array 0 and 1 only two kinds. Assume [l, r] x elements have before, then the  \sum_{i=l}^{r}a_i value should be how much? Smart you are not immediately think of, and this should be  \frac{x}{2}. It should now reflect, first of all we need to calculate and prefix.

Sample data analysis

The input sample data is [010,001,100], and then the corresponding prefix is ​​[0011112333]. Because we want the same questions asked of boys and girls, means that the result must be an even number. Nine sample data, the maximum length of only 8. 2,4,6,8 idea is if we can find a subsequence. Let's draw a table to simulate what the process of violence computing.

l r Number of people I [R] -sum [i-1] meets the? Remark
2 9 8 3-0=3  
1 8 8 3-0=3  
4 9 6 3-1=2  
3 8 6 3-1=2  
2 7 6 3-0=3 6 may not need to verify the composition 4

As shown in the above table, the longest final sequence 6.

Range of data analysis

According to data provided by the scope of the topic, we can know the maximum value of n is 10000, so the  O (n ^ {2}) complexity of the algorithm is no way to pass. Due to the small amount of data, we can subtract sticks, referring to the above idea, barely can. According to the most unfavorable principle, we can know that the complexity of the algorithm is changed  O(\frac{1}{2}n^{2})(actually no such complexity).

Algorithm optimization

Can the above O (nlogn) optimization algorithm is O (n)?

We need a simple processing of the raw data. Original data, 0 is represented by girls, boys indicated by 1, to which we will symmetric, i.e. represented by -1 girls, boys represented by 1, so long as the pair of boys and girls, and the corresponding zero. We also analyzed using the following sample data:

No. 1 2 3 4 5 6 7 8 9
Raw data 0 1 0 0 0 1 1 0 0
Modified data -1 1 -1 -1 -1 1 1 -1 -1
Prefix and -1 0 -1 -2 -3 -2 -1 -2 -3

After the data is modified, it is assumed [l, r] x have elements before, if the number of boys and girls as much as this is bound \sum_{i=l}^{r}a_i, i.e. sum [r] = sum [l -1].

Therefore, we also calculated the prefix and establish a hash table, as shown above, we can obtain the following hash table.

As shown above, the first column of the data value is a hash table, the hash value back to the corresponding prefix and index. We can see the following information:

1, only a prefix and data is 0, and therefore certainly not composed of men and women combined.

2, there are three prefixes, and data is -1, and the subscript prefixes minimum 1, maximum prefix and index of 7, 6 indicates a combination of male and female individuals can be composed, i.e. the original data array subscript 2 to 7, corresponding to 1 00011, which is 3 men and 3 women.

3, there are three prefixes, and data is -2, the smallest subscript prefixes and 4, and the subscript prefixes maximum of 8, 4 indicates a combination of male and female individuals can be composed, i.e. the original data array index 5 to 8, corresponding to 0 110, which is 2 men and 2 women.

4, there are two prefixes, and -3 data, the prefix and the minimum index of 5, and the subscript prefixes maximum 9, 4 indicates a combination of male and female individuals can be composed, i.e. the original data array subscript 6 to 9, corresponds to 1100, which is 2 men and 2 women.

AC reference code

Violence reduction achieved sticks

If the amount of data exceeds 1e5, estimated that this algorithm is powerless. To get the part TLE.

#include <cstdio>
using namespace std;

const int MAXN = 1e5+4;
int nums[MAXN];//原始数据
int qzh[MAXN];//前缀和

int main() {
    int n;
    scanf("%d", &n);

    for (int i=1; i<=n; i++) {
        scanf("%d", &nums[i]);
        qzh[i] = qzh[i-1]+nums[i];
    }

    /*
    i表示构造长度
    j表示起点
     */
    int m= (1==n%2) ? n-1 : n;
    for (int i=m; i>=2; i-=2) {
        //n,n/2,n/4,...,4,2 的长度进行验证
        for (int j=n; j-i>=0; j--) {
            if ((qzh[j]-qzh[j-i])*2==i) {
                printf("%d\n", i);
                return 0;
            }
        }
    }
    printf("0\n");

    return 0;
}

Optimization algorithm

We know the internal unordered_map achieved using a hash table, so we can use this type of STL.

#include <cstdio>
#include <unordered_map>

using namespace std;

const int MAXN = 1e5+4;
int nums[MAXN];//前缀和

int main() {
    int n;
    scanf("%d", &n);

    unordered_map<int, int> hash;
    hash[0] = 0;//特例处理
    int ans = 0;
    for (int i=1; i<=n; i++) {
        scanf("%d", &nums[i]);
        if (0==nums[i]) {
            nums[i] = -1;
        }
        nums[i] += nums[i-1];//构造前缀和

        if (hash.find(nums[i]) == hash.end()) {
            //第一次出现,增加到哈希表中
            hash[nums[i]] = i;
        } else if (i-hash[nums[i]]>0 && i-hash[nums[i]]>ans) {
            //出现过
            ans = i-hash[nums[i]];
        }
    }

    printf("%d\n", ans);

    return 0;
}

Use unordered_map, pay attention to some specific examples. If the input is

4
1 0 0 1

 

 

Published 235 original articles · won praise 289 · Views 1.07 million +

Guess you like

Origin blog.csdn.net/justidle/article/details/104848187