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. mysql DATE_FORMAT 函数使用

    使用DATE_FORMAT 查询数据库中时间类型显示 SELECT a.name ,a.uuid ,p.assess_price as assessPrice ,p.assess_date as as ...

  2. Linux中命令备份mysql形成文件

    基于龙哥(Thomas)的总结: mysqldump -u 用户名 -p密码 数据库名>bbs87.sql | tar -zcvf bbs87.tar.gz bbs87.sql 通过词条命令可以 ...

  3. ElementUI el-table 表格 行选择框改为单选

    实现方法 首先,表格加一列 <el-table-column type="selection" width="55"></el-table-c ...

  4. Cisco AP-格式化AP

    故障情况:APC11-AP04#sho capwap ip config LWAPP Static IP ConfigurationIP Address         172.17.239.204I ...

  5. APDL获取节点和单元的结果

    目录 1. 获取节点结果 1.1 获取节点应力结果 1.2 获取节点位移结果 1.3 获取节点应变结果--总应变 1.4 获取节点应变结果--弹性应变 1.5 获取节点应变结果--塑性应变 1.6 获 ...

  6. [02]Sort选择排序

    选择排序 算法速度:通过大O表示法表示,O(n),n是操作数,表示算法执行的次数: 数组:是有序的元素序列:若将有限个类型相同的变量的集合命名,那么这个名称为数组名: 链表:是一种物理存储单元上非连续 ...

  7. Jquery 事件 文本框常用

    1.只许输入类型 //只能输入整数和小数 function txtKeyUpDecimal(txtName) { getID(txtName).keyup(function(){ //keyup事件处 ...

  8. floyd的魔改应用——洛谷P2419 [USACO08JAN]牛大赛Cow Contest 题解

    想找原题请点击这里:传送门 原题: 题目背景 [Usaco2008 Jan] 题目描述 N ( ≤ N ≤ ) cows, conveniently numbered ..N, are partici ...

  9. 同一条sql insert 有时快有时慢 引发的血案

    同一条sql语句,为什么有时插入块,有时插入慢原因剖析 背景:同一条sql ,有时插入时间几毫秒,有时插入时间几十毫秒,为什么? Sql角度:简单insert 表角度: 一个主键 系统参数角度: 开启 ...

  10. 操作系统OS,Python - 生产者消费者模型

    1. 缓冲区(此处用阻塞队列充当),解决消费者和生产者强耦合问题.(生产者和消费者不直接通信) 2. 通过平衡生产者线程和消费者线程,来提高程序整体处理数据速度. 3. 在并发编程中该模式能解决大多数 ...