zoukankan      html  css  js  c++  java
  • B. Uniqueness 删除最小区间内的元素使得剩余元素唯一

    B. Uniqueness

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given an array a1,a2,,ana1,a2,…,an. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct.

    In other words, at most one time you can choose two integers ll and rr (1lrn1≤l≤r≤n) and delete integers al,al+1,,aral,al+1,…,ar from the array. Remaining elements should be pairwise distinct.

    Find the minimum size of the subsegment you need to remove to make all remaining elements distinct.

    Input

    The first line of the input contains a single integer nn (1n20001≤n≤2000) — the number of elements in the given array.

    The next line contains nn spaced integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) — the elements of the array.

    Output

    Print a single integer — the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print 00.

    Examples
    input
    Copy
    3
    1 2 3
    
    output
    Copy
    0
    
    input
    Copy
    4
    1 1 2 2
    
    output
    Copy
    2
    
    input
    Copy
    5
    1 4 1 4 9
    
    output
    Copy
    2
    
    Note

    In the first example all the elements are already distinct, therefore no subsegment needs to be removed.

    In the second example you can remove the subsegment from index 22 to 33.

    In the third example you can remove the subsegments from index 11 to 22, or from index 22 to 33, or from index 33 to 44.

    题意:删除一段连续的区间,使得剩下的所有元素都是唯一的,求满足要求的最小区间长度

    题解:逆向考虑,从左边开始[1,i ]最多可以取x个不同的数,然后从n到 i+1 最多可以连续取y个不同的数,不断枚举 i ,取cnt=max( cnt,x+y) 的最大值,n - cnt就是可以删除的最小长度

    #include<iostream>
    #include<algorithm>
    #include<math.h>
    #include<map>
    using namespace std;
    int a[3200];
    map<int,int>mp;
    int main()
    {
        int n,ans=0;
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        for(int i=0;i<=n;i++)
        {
            mp.clear();
            int cnt=0;
            for(int j=1;j<=i;j++)
            {
                if(mp[a[j]]==0)
                {
                    mp[a[j]]=1;
                    cnt++;
                }
                else
                    break;
            }
    
            for(int j=n;j>i;j--)
            {
                if(mp[a[j]]==0)
                {
                    mp[a[j]]=1;
                    cnt++;
                }
                else
                    break;
            }
            ans=max(ans,cnt);
        }
        cout<<n-ans<<endl;
        return 0;
    }
  • 相关阅读:
    工信部计算机系统集成资质(高级)项目经理
    cout cerr clog
    Cstyle 字符串小示例
    泛型<编程>:基于策略的basic_string实现
    string中c_str()、data()、copy(p,n)函数的用法
    C++引用与const引用比较
    深入了解scanf/getchar/gets/cin等函数(转载)
    使用ifstream和getline读取文件内容[c++]
    const参数,const返回值与const函数 .
    C++ limits头文件的用法(numeric_limits)
  • 原文地址:https://www.cnblogs.com/-citywall123/p/11574730.html
Copyright © 2011-2022 走看看