Educational Codeforces Round 35 A. Nearest Minimums【预处理】

【题目链接】:

Educational Codeforces Round 35 (Rated for Div. 2)

A. Nearest Minimums
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

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

【题意】:给出一个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 ;
}

预处理

上一篇:MySQL 按日期分表


下一篇:为github帐号添加SSH keys(Linux和Windows)