# 未排列的n个数组元素的最大间距

#include <iostream>
using namespace std;

template<class T>
int maxi(int n, T a[])
{
int k = 1;
for(int i=2; i <=n; i++)
{
if(a[i]>a[k])
{
k = i;
}
}
return k;
}

template<class T>
int mini(int n, T a[])
{
int k = 1;
for(int i=2; i <=n; i++)
{
if(a[i]<a[k])
{
k = i;
}
}
return k;
}

double maxgap(int n, double a[])
{
double mina = a[mini(n, a)], maxa = a[maxi(n, a)];
int *count = new int[n+1];
double *low = new double[n+1];
double *high = new double[n+1];

int i;
for(i=1; i<=n; i++)
{
count[i] = 0;
low[i] = maxa;
high[i] = mina;
}

for(i=1; i<=n; i++)
{
int bucket = (int)(a[i]-mina)/(maxa-mina)*(n-1)+1;
cout << i << "  " << bucket <<endl;
count[bucket]++;
if(low[bucket] > a[i]) low[bucket] = a[i];
if(high[bucket] < a[i]) high[bucket] = a[i];
}

for(i=1; i<=n; i++)
{
cout << low[i] << "  " << high[i] << endl;
}

double temp = 0, left = high[1];
for(i=2; i<n; i++)
{
if(count[i])
{
double gap = low[i] - left;
if(gap>temp) temp = gap;
left = high[i];
}
}
cout << temp << endl;
delete [] count;
delete [] low;
delete [] high;
return temp;
}

int _tmain(int argc, _TCHAR* argv[])
{
int n;
cin >> n;
double *a = new double[n+1];
for(int i=1; i<=n; i++)
cin >> a[i];
cout << maxgap(n, a) << endl;
delete [] a;
system("pause");
return 0;
}
/*5 2.3 3.1 7.5 1.5 6.3
1  1
2  1
3  5
4  1
5  3
1.5  3.1
7.5  1.5
6.3  6.3
7.5  1.5
7.5  7.5
3.2
3.2*/

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120