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 (1≤l≤r≤n1≤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 (1≤n≤20001≤n≤2000) — the number of elements in the given array.

The next line contains nn spaced integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤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[];
map<int,int>mp;
int main()
{
int n,ans=;
cin>>n;
for(int i=;i<=n;i++)
cin>>a[i];
for(int i=;i<=n;i++)
{
mp.clear();
int cnt=;
for(int j=;j<=i;j++)
{
if(mp[a[j]]==)
{
mp[a[j]]=;
cnt++;
}
else
break;
} for(int j=n;j>i;j--)
{
if(mp[a[j]]==)
{
mp[a[j]]=;
cnt++;
}
else
break;
}
ans=max(ans,cnt);
}
cout<<n-ans<<endl;
return ;
}

B. Uniqueness 删除最小区间内的元素使得剩余元素唯一的更多相关文章

  1. 求包含每个有序数组(共k个)至少一个元素的最小区间

    title: 求包含每个有序数组(共k个)至少一个元素的最小区间 toc: false date: 2018-09-22 21:03:22 categories: OJ tags: 归并 给定k个有序 ...

  2. YTU 2986: 删除区间内的元素(线性表)

    2986: 删除区间内的元素(线性表) 时间限制: 1 Sec  内存限制: 2 MB 提交: 8  解决: 3 题目描述 若一个线性表L采用顺序存储结构,其中元素都为整数.设计一个算法,删除元素值在 ...

  3. LOJ #6279. 数列分块入门 3-分块(区间加法、查询区间内小于某个值x的前驱(比其小的最大元素))

    #6279. 数列分块入门 3 内存限制:256 MiB时间限制:1500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论 3   题目描述 给 ...

  4. LOJ #6278. 数列分块入门 2-分块(区间加法、查询区间内小于某个值x的元素个数)

    #6278. 数列分块入门 2 内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论 6   题目描述 给出 ...

  5. HDU 3264 区间内的最大最小之差

    题目链接:http://poj.org/problem?id=3264 题目大意:在给定一堆牛的数量以及其高度的时候,每次给定一段区间,求这个区间内最高的牛和最矮的牛的高度之差为多少. 可以直接利用R ...

  6. 转:最小区间:k个有序的数组,找到最小区间使k个数组中每个数组至少有一个数在区间中

    转:http://www.itmian4.com/thread-6504-1-1.html 最小区间原题 k个有序的数组,找到最小的区间范围使得这k个数组中,每个数组至少有一个数字在这个区间范围内.比 ...

  7. hdu3437 划分树 区间内小于第K大的值得和

    Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  8. CSS样式设置语法全解,样式优先级、值和单位、字体、文本、块级元素,行内元素,替换元素、非替换元素、display、float、position、table、li、光标、边距边框、轮廓、颜色背景

    全栈工程师开发手册 (作者:栾鹏) 一个demo学会css css选择器全解 css操作语法全解 CSS样式设置语法全解: 样式优先级 1. !important标记的样式 > 内联样式(sty ...

  9. 单调队列——求m区间内的最小值

    单调队列,顾名思义是指队列内的元素是有序的,队头为当前的最大值(单调递减队列)或最小值(单调递增序列),以单调递减队列为例来看队列的入队和出队操作: 1.入队: 如果当前元素要进队,把当前元素和队尾元 ...

随机推荐

  1. TOMCAT中文信息乱码改为GBK

    # Licensed to the Apache Software Foundation (ASF) under one or more# contributor license agreements ...

  2. C语言:将字符串中的字符逆序输出,但不改变字符串中的内容。-在main函数中将多次调用fun函数,每调用一次,输出链表尾部结点中的数据,并释放该结点,使链表缩短。

    //将字符串中的字符逆序输出,但不改变字符串中的内容. #include <stdio.h> /************found************/ void fun (char ...

  3. C# 动态类型 以及 脚本调用(scriptRunTime)

    在使用中让我感觉 好像 JavaScript中的 动态类型囧 先看动态类型 class Program { static void Main(string[] args) { var streamRe ...

  4. Manjaro 与 Windows 双系统时间相差8小时

    Manjaro 与 Windows 双系统时间相差8小时 产生原因: Windows 将本地硬件时间作为设定时区(如 UTC+8)的区时,而 Linux 默认将其作为 UTC 时间. 解决方式: 让 ...

  5. P1893山峰瞭望

    传送门 看完这个题,大家都懂意思吧,然后代码呢,emmmmm #include <bits/stdc++.h> using namespace std; const int maxn = ...

  6. 生成唯一的ID

    public class UniqueId { public static String getUUId(){ ; int hashCodeV = UUID.randomUUID().toString ...

  7. Docker容器的操作命令

    Docker容器的操作命令 写该文章时候的初衷是在使用Docker for windows 的时候遇到的一些问题.起初我在运行好docker客户端以后,创建了一个.NET Core应用发布到docke ...

  8. Qt实现通用模板单例

    #include <QAtomicPointer> #include <QMutex> #include <memory> using namespace std; ...

  9. SpringSecurity配置,简单梳理

    生活加油:摘一句子: “我希望自己能写这样的诗.我希望自己也是一颗星星.如果我会发光,就不必害怕黑暗.如果我自己是那么美好,那么一切恐惧就可以烟消云散.于是我开始存下了一点希望—如果我能做到,那么我就 ...

  10. WCF 数据传输SIZE过大

    1.当客户端调用WCF服务时,接受数据过大,可通过以下配置解决 <basicHttpBinding> <binding name="BasicHttpBinding_Wcf ...