zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 76 (Rated for Div. 2) C. Dominated Subarray

    Let's call an array tdominated by value vv in the next situation.

    At first, array tt should have at least 22 elements. Now, let's calculate number of occurrences of each number numnum in tt and define it as occ(num)occ(num). Then tt is dominated (by vv) if (and only if) occ(v)>occ(v)occ(v)>occ(v′) for any other number vv′. For example, arrays [1,2,3,4,5,2][1,2,3,4,5,2], [11,11][11,11] and [3,2,3,2,3][3,2,3,2,3] are dominated (by 22, 1111 and 33 respectevitely) but arrays [3][3], [1,2][1,2] and [3,3,2,2,1][3,3,2,2,1] are not.

    Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not.

    You are given array a1,a2,,ana1,a2,…,an. Calculate its shortest dominated subarray or say that there are no such subarrays.

    The subarray of aa is a contiguous part of the array aa, i. e. the array ai,ai+1,,ajai,ai+1,…,aj for some 1ijn1≤i≤j≤n.

    Input

    The first line contains single integer TT (1T10001≤T≤1000) — the number of test cases. Each test case consists of two lines.

    The first line contains single integer nn (1n21051≤n≤2⋅105) — the length of the array aa.

    The second line contains nn integers a1,a2,,ana1,a2,…,an (1ain1≤ai≤n) — the corresponding values of the array aa.

    It's guaranteed that the total length of all arrays in one test doesn't exceed 21052⋅105.

    Output

    Print TT integers — one per test case. For each test case print the only integer — the length of the shortest dominated subarray, or 1−1 if there are no such subarrays.

    Example
    input
    Copy
    4
    1
    1
    6
    1 2 3 4 5 1
    9
    4 1 2 4 5 4 3 2 1
    4
    3 3 3 3
    
    output
    Copy
    -1
    6
    3
    2
    
    Note

    In the first test case, there are no subarrays of length at least 22, so the answer is 1−1.

    In the second test case, the whole array is dominated (by 11) and it's the only dominated subarray.

    In the third test case, the subarray a4,a5,a6a4,a5,a6 is the shortest dominated subarray.

    In the fourth test case, all subarrays of length more than one are dominated.

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=2e5+10;
    int n,a[maxn],pre[maxn];
    int main() {
        int t;
        scanf("%d",&t);
        while(t--) {
            scanf("%d",&n);
            int ans=0x3f3f3f3f;
            for(int i=1; i<=n; i++) pre[i]=-1;
            for(int i=1; i<=n; i++) {
                scanf("%d",&a[i]);//先输入
                if(pre[a[i]]!=-1)//判断之前有没有出现过
                    ans=min(ans,i-pre[a[i]]+1);//如果出现过,取到上一个点的距离并取最小
                pre[a[i]]=i;//标记此时点的距离
            }
            printf("%d
    ",ans==0x3f3f3f3f?-1:ans);
        }
    }
    //计算每两个相同元素的距离对ans 取min
  • 相关阅读:
    Jenkins结合.net平台综合应用之通过SSH方式拉取代码
    Jenkins结合.net平台综合之监听git仓库并自动摘取最新代码编译
    XCode插件因为升级不能用了怎么办?几个步骤教你搞定
    如何写一个FMDB帮助类?看看runtime吧
    ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(五) 补充:历史记录 和 消息提醒
    【转】iOS的APP资源,开源的哦
    活到老学到老:iOS开发中的基础知识(一)
    融云SDK:获取用户Token的方法
    对接融云即时通讯组件SDK,轻松实现App聊天室
    【iOS】那些年,遇到的小坑
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/11863746.html
Copyright © 2011-2022 走看看