【题目链接】:
Educational Codeforces Round 35 (Rated for Div. 2)
2 seconds
256 megabytes
standard input
standard 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.
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.
Print the only number — distance between two nearest minimums in the array.
2
3 3
1
3
5 6 5
2
9
2 1 3 5 4 1 2 3 1
3
【题意】:给出一个n个整数的数组。找出两个最小值之间的最接近的(最近的)距离。
【时间复杂度&&优化】:O(1e5)
【分析】:先在读入时预处理找到最小值。再开一个On循环,当某个数初次等于最小值时记录一下他的下标赋给last,非初次出现的等于最小值的直接寻找最近距离。
【代码】:
#include <bits/stdc++.h> using namespace std; int n;
int a[];
int main(){
scanf("%d",&n);
int mi=1e9+;
for(int i=;i<n;i++){
scanf("%d",&a[i]);
mi=min(mi,a[i]);
}
int ans=1e9;
for(int la=-,i=;i<n;i++){
if(a[i]==mi){
if(la!=-){
ans=min(ans,i-la);
}
la=i;
}
}
printf("%d\n",ans);
return ;
}
预处理