原文题目
Let’s call an array of non-negative integers a1,a2,…,an a k-extension for some non-negative integer k if for all possible pairs of indices 1≤i,j≤n the inequality k⋅|i−j|≤min(ai,aj) is satisfied. The expansion coefficient of the array a is the maximal integer k such that the array a is a k-extension. Any array is a 0-expansion, so the expansion coefficient always exists.
You are given an array of non-negative integers a1,a2,…,an. Find its expansion coefficient.
Input
The first line contains one positive integer n — the number of elements in the array a (2≤n≤300000). The next line contains n non-negative integers a1,a2,…,an, separated by spaces (0≤ai≤109).
Output
Print one non-negative integer — expansion coefficient of the array a1,a2,…,an.
题意
给了一个数和该数大小的数组,求一个数k,是k满足任意情况下
k⋅|i−j|≤min(ai,aj)
求这个k值
题解
看到数据范围是300000,那么我们能想到的算法应该是在O(N)或者nlogn时间内解决,第一反应的O(n^2)暴力枚举必然是不行的。我们可以分析一下题目,要求的k是满足一切情况下的 i 和 j ,那么可以想,如果k要满足一切条件,在极端条件下,i-j是趋近于n的,而min也可以很小,又因为是小于等于,所以我们所求出的k应该越小越好,最小的一定可以满足任意情况(0);
那么,如何求到最小的k值呢?
不妨我们想,因为min(a[i],a[j])本质上仍然是一个数,而i-j也是一个数,所以我们只需要遍历on,就可以取到所有可能的min了,而i-j并不关键,因为对于一个确定的min来说,i-j的值应该越大越好,所以不妨对于一个数,我们将i-j直接取到边界值n,比较它离左右端点哪边更远就可以了。
实际上,我们不需要求min,因为我们进行on的遍历后,就可以取到数组中的每一个元素,min的所有可能的值都会被取到,我们只需要对边界值进行极限处理就可以得到最小的k了,此时的k就满足一切情况了。
#include <bits/stdc++.h>
using namespace std;
int a[300005];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,i,j=0;
long long k,t;
cin>>n;
for(i=1;i<=n;i++)
cin>>a[i];
k=a[n]/(n-1);//预处理一下一个边界,防止分母变成0
for(i=1;i<n;i++){
t=a[i]/max(n-i,i-1);
if(t<k){
j=i;
k=t;
}
}
cout<<k<<endl;
//cout<<j<<endl;
return 0;
}