Main elements of the problem study notes

What are the main elements of the problem?

Given a \ (n-\) series elements, which ensure a frequency strictly greater than the number of occurrences of \ (50 \% \) , find this number.

How to do it?

Simple bucket count

Our first thought, at least I think that is the first bucket count. The emergence of a number, put it into the bucket. Bucket count is a very basic concept, if you do not understand this, and quickly Baidu go!
Code implementation is very simple, pure hand knock it, there is no pot repair.

#include <iostream>
#include <cstdio>

int bucket[10086];

int main() {
    int n;
    std :: cin >> n;
    for(int i = 1; i <= n; i++) {
        int x;
        std :: cin >> x;
        bucket[x]++;
    }
    for(int i = 1; i <= 10086; i++)
        if(bucket[i] > n / 2) {
            cout << i << std :: endl;
            break;
        }
    return 0;
}

But the limitations of bucket count is very large, very easy to blast space, int scope of this stuff can make direct gg.

If you think of a discrete, indicating that you are still very smart, crab crab for your thumbs. But discrete not the best way, there are still a lot of things will be discrete card. We count it directly pass out of the barrel.

Well, I know some people began to call ...... Shaoanwuzao the map, I have a better plan.

Sequence

This is called a better solution? ? ? ? Crab You're way out.
I do not rush blasting step, a first number of columns of the primary element, if present, after the sort of \ (\ dfrac {n} { 2} \) several locations must be on the main element.
We have a new method:

#include <iostream>
#include <cstdio>

const int maxn = 10005;
int a[maxn];

int main() {
    int n;
    for(int i = 1; i <= n; i++)
        std :: cin >> a[i];
    
    sort(a + 1, a + n + 1);
    std :: cout << a[n / 2 + 1];
    return 0;
}

Obviously this complexity of the algorithm is \ (O (the n-\ the n-log) \) , but there is no direct \ (O (n) \) thing to do?
Really have. Before or a beginner in time to see the main elements of the problem to stop thinking here, have a chance to see the day before yesterday, this question, I have a new idea.

Cancel one another law

We put all the sequence numbers are not the same two cancel each other out, and the rest is the main element. Because the number of main elements that appear in the series strictly greater than \ (50 \% \) , it is difficult to guarantee the correctness of this approach. This method is very delicate.
Here you can use a variety of implementations, I chose stackto eat = =

#include <iostream>
#include <cstdio>

const int maxn = 10000005;
int s[maxn];
int top;

int main() {
    int n;
    scanf("%d",&n);
    while(n--) {
        int x;
        scanf("%d",&x);
        s[top++] = x;
        if(top == 1) continue;
        if(s[top - 1] != s[top - 2]) top -= 2;
    }
    printf("%d\n",s[top - 1]);
    return 0;
}

postscript

The really interesting question, also verified growing up I thought. Two years ago, I can only think \ (O (n \ log n ) \) practice, and now I was able to have an independent thinking (O (n) \) \ approach. So I progress, I will always progress. Really deep feelings.

Guess you like

Origin www.cnblogs.com/crab-in-the-northeast/p/major-element-question.html