A. Nearest Minimums
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array of n integer numbers a0, a1, …, an - 1. Find the distance between two closest (nearest) minimums in it. It is guaranteed that in the array a minimum occurs at least two times.
Input
The first line contains positive integer n (2 ≤ n ≤ 105) — size of the given array. The second line contains n integers a0, a1, …, an - 1 (1 ≤ ai ≤ 109) — elements of the array. It is guaranteed that in the array a minimum occurs at least two times.
Output
Print the only number — distance between two nearest minimums in the array.
Examples
input
2
3 3
output
1
input
3
5 6 5
output
2
input
9
2 1 3 5 4 1 2 3 1
output
3
找出最小的数,相离最小距离。
最小的数,不是最小的两个数,理解不要失误!
解题思路,两个位置移动找一个距离的最小值
#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
int a[100005];
int main()
{
int n;
scanf("%d",&n);
int minn=inf,ans=inf,k;
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]<minn)
{
minn=a[i];
k=i;
}
}
//printf("%d\n",k);
int m=k;
for(int i=k+1;i<n;i++)
{
if(a[i]==minn)
{
ans=min(ans,abs(k-i));
k=i;
}
}
printf("%d\n",ans);
return 0;
}