Luo Gu P2397: yyy loves Maths VI (mode) (mole voting algorithm)

Topic background

After the last automatic with the addition redbag good things difficult for students over yyy, yyy very angry. He fought back to the redbag a problem, but this problem he was surprised to find myself actually not, so I had to find you

Title Description

[H1] udp2: the first question because the nature of language, memory transfer after the end of the game all c / c ++ program to retest after 2.2mb. [/ H1]

He let redbag find a mode

He also specifically said that the number of times the public appeared more than half

A total number of n, but also to ensure there is

n<=2000000

And each number <2 ^ 31-1

Input Format

A first line integer n

The second line n integers

Output Format

A row, the number of public

Sample input and output

Input # 1
5
2 3 3 3 3
Output # 1
3

Description / Tips

Time limit 1s

Space constraints 3.5M (you're not wrong 3.5M)

Someone wants water, but I tell you this is not enough space

// kkksc03 secretly say: you just output a digital bar, has 1/2 chance. But this is more than music game, it is worth not worth it as you see fit. So it is best to think positive solution.

Thinking

Boyer-Moore majority vote algorithm (mol voting algorithm) is a linear time constant and the algorithm complexity level space to find all the elements of a sequence number (more than the general number of occurrences).

The basic idea of ​​the algorithm:

The basic idea is very simple mole voting method, in each round voting process to find a different pair of elements from an array, remove it from the array. Such continuous deleted until no longer vote, if the array is empty, then no number of any element that appears more than half the length of the array. If there is only one element, then the element is likely to target elements. (Excerpt from https://www.jianshu.com/p/c19bb428f57a )

In any array, more than half of the total number of times a certain maximum of one

Each one element selected from the array, and setting a counter, if the counter is 0, then x is assumed that the mode for the current element num; if not zero, judging whether all the x and the hypothetical number is equal to the current element num, if equal, counter + 1, otherwise -1 counter.

If the last counter is zero, then the mode does not exist

Because the mode must exist to ensure that the title, it is required to judge the value of the last counter value needs to output only stay in the mode we assume that x is the last, namely to change the mode of the array

Similarly, you can expand the array to find the number occurs more than one-third of the number of

Code 

 1 #include <bits/stdc++.h>
 2 #define ll long long
 3 #define ull unsigned long long
 4 #define ms(a,b) memset(a,b,sizeof(a))
 5 const int inf=0x3f3f3f3f;
 6 const ll INF=0x3f3f3f3f3f3f3f3f;
 7 const int maxn=2e7+10;
 8 const int mod=1e9+7;
 9 const int maxm=1e3+10;
10 using namespace std;
11 int main(int argc, char const *argv[])
12 {
13     ios::sync_with_stdio(false);
14     cin.tie(0);
15     int n;
16     cin>>n;
17     ll x;
18     int res=0;
19     ll ans;
20     int i;
21     for(i=0;i<n;i++)
22     {
23         cin>>x;
24         if (! res)
 25              years = x;
26          if (x == years)
 27              res ++ ;
28          if (x! = Years)
 29              res-- ;
30      }
 31      cout << age << endl;
32      return  0 ;
33 }

 

Guess you like

Origin www.cnblogs.com/Friends-A/p/11368412.html