朴素版:
d最大的可能是a[i]-a[i-1]的最小间隔
求出之后 一直从大到小遍历
看哪一个符合
#include <iostream>
#include <algorithm>
using namespace std;
const int n = 100005;
int a[n];
int N;
int f(int d)
{
int n = (a[N - 1] - a[0]) / d + 1;
int x = a[0];
int index = 0;
while (x <= a[N - 1])
{
if (x == a[index])
index++;
x += d;
}
return (index == N ? n : 0);
}
int main()
{
cin >> N;
for (int i = 0; i < N; i++)
scanf("%d", &a[i]);
sort(a, a + N);
int l = 1, r = 1e8;
for (int i = 0; i < N - 1; i++)
r = min(a[i + 1] - a[i], r);
if (r == 1)
{
int n = (a[N - 1] - a[0]) / 1 + 1;
cout << n;
return 0;
}
int ans = 0;
while (r--)
{
int t = f(r + 1);
if (t)
{
ans = t;
break;
}
}
cout << ans;
cout << " " << r + 1 << endl;
return 0;
}