Article From:https://www.cnblogs.com/pprp/p/9688481.html

122333456012345678

After sorting, it is intended to solve the number of intermediate positions, that is, the number of first three (record left and right boundary 3,5)

Then recursive solution is carried out on the left side of the left side and recursively solved on the right side of the right border.

In this process there is an optimization, if the number on the left is not enough to be greater than the current maximum multiplicity, then there is no need to do statistics on the left side of the content, the right side is the same.

Below is the code.

#include <bits/stdc++.h>

using namespace std;

//Find the same number as a[mid] from left to right./ / the left and right boundaries are low, highVoid solve (int s[], int n, int& L, int& R){IntMid = n/2;For (L = 0; l < n; l++) {If (s[l] = = s[mid])Break;}For (r = l+1; R < n; r++) {If (s[r] = s[mid]) {Break;}}}VoiD _MaxCnt (int & mid, int & maxCnt, int a[], int int){Int l, R;Solve (a, N, l, R);IntNum = n/2;Int CNT = R-L;/ / if the mode is current, update it.If (CNT > maxCnt) {MaxCnt = cnt;Mid = a[num];}/ / recursive query on the left side.If (l+1 > maxCnt) {_MaxCnt (mid, maxCnt, a, l+1);}/ / recursive query on the right side.If (N-R > maxCnt) {_MaxCnt (mid, maxCnt, a+r, N-R);}}Int main() {Int a[] = {1,2,3,3,3,4,9,6,7,7,7,7,9,5,10,10,12,12,14,14,15,14,31,23,5,23,4,43,3,2,3,43,2,1,1,34,2,3,2,2,2,22,0,2,12,5,5,5,5,5,5,5,6,5};Int n= sizeof (a) /sizeof (a[0]);Sort (a, a+n);Int maxCnt = 0;Int num = 0;_MaxCnt (Num, m)AxCnt, a, n);Cout < < num < < "<"; < maxCnt < < <Return 0;}

Leave a Reply

Your email address will not be published. Required fields are marked *