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.

题目链接:http://codeforces.com/contest/1208/problem/B

题解:建立一个map,来记录某个值出现的次数。先从后往前遍历,找到第一个重复的值,记录该位置。然后,从前后遍历,如果当前的这个值在后面出现过,我就需要找到后米娜那个值出现的位置,然后记录最小值,重复操作n次。还是看代码理解把。。。

#include <iostream>
#include <cstdio>
#include <map> using namespace std; const int maxn = 2e3+; map<int, int> mp;
int arr[maxn]; int main() {
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &arr[i]);
}
int j = n;
while(j >= ) { //找到第一个重复的值的位置
if(mp[arr[j]] >= ) {
break;
}
mp[arr[j]]++;
j--;
}
int ans = INT_MAX;
for(int i = ; i <= n; i++) {
ans = min(ans, j - i + );
mp[arr[i]]++;
while(mp[arr[i]] >= && j <= n) { //找到后面这段中已经匹配过,且与当前值相同数的位置
j++;
mp[arr[j]]--;
}
if(mp[arr[i]] >= ) { //如果前面的操作都做完了,还有相同的值,就需要跳出,可以看下面这组样例来模拟
//6
//9 6 4 4 9 4
break;
}
}
printf("%d\n", ans);
return ;
}

B. Uniqueness(尺取)的更多相关文章

  1. Gym 100703I---Endeavor for perfection(尺取)

    题目链接 http://codeforces.com/problemset/gymProblem/100703/I Description standard input/outputStatement ...

  2. NOJ 1072 The longest same color grid(尺取)

    Problem 1072: The longest same color grid Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit in ...

  3. hdu 4123 Bob’s Race 树的直径+rmq+尺取

    Bob’s Race Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Probl ...

  4. Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E. Cubes (尺取)

    题目链接:http://codeforces.com/problemset/problem/180/E 给你n个数,每个数代表一种颜色,给你1到m的m种颜色.最多可以删k个数,问你最长连续相同颜色的序 ...

  5. poj2566尺取变形

    Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronaut ...

  6. poj2100还是尺取

    King George has recently decided that he would like to have a new design for the royal graveyard. Th ...

  7. hdu 6231 -- K-th Number(二分+尺取)

    题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an ...

  8. Codeforces 939E Maximize! (三分 || 尺取)

    <题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-me ...

  9. cf1121d 尺取

    尺取,写起来有点麻烦 枚举左端点,然后找到右端点,,使得区间[l,r]里各种颜色花朵的数量满足b数组中各种花朵的数量,然后再judge区间[l,r]截取出后能否可以供剩下的n-1个人做花环 /* 给定 ...

  10. HDU 5178 pairs【二分】||【尺取】

    <题目链接> 题目大意: 给定一个整数序列,求出绝对值小于等于k的有序对个数. 解题分析: $O(nlong(n))$的二分很好写,这里就不解释了.本题尺取$O(n)$也能做,并且效率很不 ...

随机推荐

  1. JS使用MD5加密

    MD5加密JS代码 /* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algor ...

  2. 初学java1 数据类型

    java数据类型 分为8种 整型 byte 8位 short 16位 int 32位 long 64位 字符型 char 必需为单引号'' 且只能有一个字符 浮点型 float double 布尔类型 ...

  3. 3、java基础:抽象类与接口的区别

    抽象类 我们都知道在面向对象的领域一切都是对象,同时所有的对象都是通过类来描述的,但是并不是所有的类都是来描述对象的.如果一个类没有足够的信息来描述一个具体的对象,而需要其他具体的类来支撑它,那么这样 ...

  4. 转载一篇有关于diff的文章,方便以后复习

    本文章是转载的,为了方便以后复习,特地记录一下.他人请去原地址观看!!! 文章原地址:http://www.ruanyifeng.com/blog/2012/08/how_to_read_diff.h ...

  5. react请求接口数据是在componentDidMount 还是componentWillMount周期好

    如果你要获取外部数据并加载到组件上,只能在组件"已经"挂载到真实的网页上才能作这事情,其它情况你是加载不到组件的.componentDidMount方法中的代码,是在组件已经完全挂 ...

  6. <s:bean>标签的使用

    今天在使用<s:bean>时出了一个问题,感觉有意思的就记录下来吧,以备学习 在使用这个标签的时候需要注意两个事项: 1.<s:bean>的三个属性 id,name,var,在 ...

  7. HCIP DAY2

    OSPF协议的基本特点: 支持无类域间路由(CIDR) vlsm NA 无路由自环 收敛速度快 使用IP组播放收发协议数据 支持多条等值路由 静态路由 动态路由 等价路由 浮动路由 支持协议报文的认证 ...

  8. mount命令解析

    可以参考两位大神的理解 Linux mount 命令 Linux的mount命令详解

  9. 虚拟机ipv6环境搭建操作指南

      一.vmware的相关配置 (1)点击编辑,选择虚拟网络编辑器 (2)选择带NAT模式的VMnet8网络,点击NAT设置 (3)在NAT设置中启用IPV6 (4)设置好后,点击应用 (5)再选择镜 ...

  10. 学习使用Django一 安装虚拟环境

    以上环境可以先在虚拟机上操作,熟练之后再正式机操作!!! 再学习Djangj之前,先讲个小概念,虚拟环境     记得刚刚开始学习Python的时候,往往是用的那个包,就Cmd 上 直接输入“pip ...