【Educational Codeforces Round 35 A】 Nearest Minimums

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

找出最小的数字的位置。 最近的肯定是相邻的某对。

【代码】

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5;

int n;
int a[N+10],mi;

int main(){
	#ifdef LOCAL_DEFINE
	    freopen("rush_in.txt", "r", stdin);
	#endif
	ios::sync_with_stdio(0),cin.tie(0);
    cin >> n;
    for (int i = 1;i <= n;i++) cin >> a[i];
    mi = a[1];
    for (int i = 2;i <= n;i++) mi = min(mi,a[i]);
    int ans = -1,pre=-1;
    for (int i = 1;i <= n;i++){
        if (a[i]==mi){
            if (pre==-1){
                pre = i;
            }else{
                if (ans==-1){
                    ans = i-pre;

                }else{
                    ans = min(ans,i-pre);
                }
                pre = i;
            }
        }
    }
    cout << ans << endl;
	return 0;
}
posted @ 2017-12-29 10:17  AWCXV  阅读(152)  评论(0编辑  收藏  举报