zoukankan      html  css  js  c++  java
  • 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[100009];
    int main(){
        scanf("%d",&n);
        int mi=1e9+7;
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
            mi=min(mi,a[i]);
        }
        int ans=1e9;
        for(int la=-1,i=0;i<n;i++){
            if(a[i]==mi){
                if(la!=-1){
                    ans=min(ans,i-la);
                }
                la=i;
            }
        }
        printf("%d
    ",ans);
        return 0;
    }
    预处理
  • 相关阅读:
    数据降维和可视化
    聚类
    支持向量机的实现
    支持向量机核函数
    支持向量基
    倾斜类误差度量
    构建垃圾邮件分类器
    POJ 2955 Brackets (区间dp入门)
    POJ 3126 Prime Path (bfs+欧拉线性素数筛)
    POJ 1426 Find The Multiple (dfs??!!)
  • 原文地址:https://www.cnblogs.com/Roni-i/p/8143752.html
Copyright © 2011-2022 走看看