【题目链接】:
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个整数的数组。找出两个最小值之间的最接近的(最近的)距离。
#include<bits/stdc++.h>
using namespace std;
int a[106],b[106];
int main()
{
int n;
while(~scanf("%d",&n))
{
int minx=0x3f3f,mind=0x3f3f,k=0;
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
minx=min(minx,a[i]); //找出最小值!
}
for(int i=0; i<n; i++)
{
if(a[i]==minx)
{
b[k++]=i; //开辟另外一个数组存最小值的位置
}
}
//printf("%d",k);
for(int i=1; i<k; i++)
{
mind=min(mind,b[i]-b[i-1]);
}
printf("%d\n",mind);
}
}