// 用分治法求众数
//参考:http://blog.csdn.net/core__code/article/details/47385045
#include <iostream>
#include <cstdio>
using namespace std;
void split(int s[], int n, int &l, int &r) //here should have a low limit, because the split for the top half will don't need to travel the whole array;
{
int middle = n / 2;
for(l=0; l<n; l++)
{
if(s[l]==s[middle])
{
break;
}
}
for(r=middle+1; r<n; r++)
{
if(s[r]!=s[middle])
{
break;
}
}
return;
}
void getMaxCount(int s[], int n, int &maxCount, int &targetValue) //high is not the upper of array, but the contain of array;
{
int l, r; //l is the left edge of middle elements, r is right
split(s, n, l, r);
int middle = n / 2;
if(r-l>maxCount)
{
maxCount = r - l;
targetValue = s[middle];
}
if(maxCount<l) //if the number on the left is less than the number of already getting the number of maxcount, the recurrence will not need to continue
{
getMaxCount(s, l, maxCount, targetValue); //l is impossible to arrive
}
if(maxCount<n-r)
{
getMaxCount(s+r, n-r, maxCount, targetValue);
}
return;
}
int main(void)
{
int s[] = {1, 2, 2, 2, 3, 3, 3, 3, 3, 5, 6, 6, 6, 6, 6, 6};
int n = sizeof(s)/sizeof(s[0]);
int maxCount = 0;
int targetValue = 0; //this is meaningless, because the maxCount is zero, the showing times of every num is bigger than one
getMaxCount(s, n, maxCount, targetValue);
cout << targetValue << " " << maxCount;
return 0;
}