You are given an array a1,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 l and r (1≤l≤r≤n) and delete integers al,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 n (1≤n≤2000) — the number of elements in the given array.
The next line contains n spaced integers a1,a2,…,an (1≤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 0.

Examples
inputCopy
3
1 2 3
outputCopy
0
inputCopy
4
1 1 2 2
outputCopy
2
inputCopy
5
1 4 1 4 9
outputCopy
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 2 to 3.
In the third example you can remove the subsegments from index 1 to 2, or from index 2 to 3, or from index 3 to 4.

#include<bits/stdc++.h>
using namespace std;
#define maxn 100010
typedef long long ll;
int n,a[maxn],b[maxn],vis[maxn];
bool check(int len){memset(vis,0,sizeof(vis));for(int i=1;i<=n;i++) vis[a[i]]++;for(int i=1;i<=n-len+1;i++){for(int j=i;j<=i+len-1;j++) vis[a[j]]--;bool flag=true;for(int j=1;j<=n;j++){if(vis[a[j]]>=2){flag=false;break;}}if(flag) return true;for(int j=i;j<=i+len-1;j++) vis[a[j]]++;}return false;
}
int main(){scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i];sort(b+1,b+n+1);int cnt=unique(b+1,b+n+1)-(b+1);for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+cnt+1,a[i])-b;int l=0,r=n,ans=0;while(l<=r){int mid=(l+r)>>1;if(check(mid)){ans=mid;r=mid-1;}else l=mid+1;}printf("%d\n",ans);return 0;
}

Uniqueness相关推荐

  1. ruby on rails validates uniqueness

    最近在处理一个小功能,每个元素可以有多个图片,每个图片的name表示了它是背景图还是海报图, 需要对每个元素的图片name做一个唯一性验证,一个元素不能添加两个海报图, 需要使用的是validates ...

  2. Uniqueness(思维 or 二分 or 暴力)

    题目链接:B. Uniqueness 题意:n (1 - 2000) 个数,只删除一个子串使得数组中没有重复元素,求删除的子串长度. 思路(参考博客cf1208B B. Uniqueness): 逆向 ...

  3. FDS-HW10 Uniqueness of MST

    Uniqueness of MST 思路分析 给定N个节点,M条边,每条边的端点以及边的权重已知,求最小生成树(MST)的权重,以及权重是否唯一,如果不存在最小生成树,则求有几个部分. 记录M条边的信 ...

  4. PAT | T1016 Uniqueness of MST

    测试点3答案错误,31分 #include <iostream> #include <vector> #include <algorithm> #include & ...

  5. PAT顶级 1016 Uniqueness of MST (35分)(判断最小生成树是否唯一)

    添加链接描述 Given any weighted undirected graph, there exists at least one minimum spanning tree (MST) if ...

  6. Oracle常用傻瓜问题1000问

    1. Oracle安装完成后的初始口令? internal/oracle sys/change_on_install system/manager scott/tiger sysman/oem_tem ...

  7. ADPRL - 近似动态规划和强化学习 - Note 3 - Stochastic Infinite Horizon Problem

    Stochastic Infinite Horizon Problem 3.Stochastic Infinite Horizon Problem 定义3.1 无限范围的马尔可夫决策过程 (Marko ...

  8. MCSE2003学习之三

    安装WIN XP XP的系统中,32位的系统最大支持的RAM为4G,而64位的最大支持为16G. 在有关WIN XP和升级安装中:WIN98,ME,NT4.0 SP5及WIN2000 PRO可以直接升 ...

  9. 主键SQL教程–如何在数据库中定义主键

    Every great story starts with an identity crisis. Luke, the great Jedi Master, begins unsure - " ...

最新文章

  1. 智能物联网(AIoT,2020年)(下)
  2. [转] composer - 文档 - 命令行
  3. xml方式实现aop-切点表达式的写法
  4. 红帽公布2017财年第一季度财报
  5. 推荐系统 | 引用量超过1000的52篇经典论文
  6. linux内核中读写文件
  7. 【Siddhi】SiddhiQL Guide 3.x
  8. 在ASP.NET的服务器端使用message box(Message box Server side in ASP.Net)
  9. 未联网环境下:离线安装Zsh和Oh My Zsh
  10. 拉格朗日插值的优缺点_如何直观地理解拉格朗日插值法?
  11. Jenkins Pipeline 手记(1)—— 什么是CPS编程
  12. 【ionic框架bug】slideBox在使用model或pop后width变为0的解决方案
  13. 特殊教育学校计算机教学计划,2017年特殊教育学校教学计划
  14. C语言估算数学常量e,数学常数e
  15. 证件照怎么裁剪?国考证件照的尺寸是多少?
  16. def序列化器---视图序列化反序列化
  17. 为什么开发效率这么低,时间都去哪了?
  18. 解密新晋信息窃取木马Spymel
  19. 系统出现“预体验成员内版本遇到 问题”错误的处理(亲身经历)
  20. 阿里云的这群疯子--深度好文成功绝非偶然

热门文章

  1. Oracle行转列pivot
  2. 实现java的outline功能_outline使用方法,outline与border的区别
  3. 浅谈从 AI 与元宇宙双向奔赴的角度看待 DeepBrain 深脑链的市场应用
  4. labelme批量制作数据集教程
  5. linux 没有音频输出的解决方式
  6. Java岗大厂面试百日冲刺 - 日积月累,每日三题【Day40】—— 数据库7
  7. 华为mate20参数表_华为Mate20售价5000起 华为Mate20配置参数详解
  8. 如何将华为C8650刷回原系统?
  9. 重磅!《命令与征服》和《红色警戒》源代码在GitHub公布了
  10. 除了头像,还有哪些现象级NFT?| 国庆特辑