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. (七)Redis之Keys的通用操作

    package myRedis01; import java.util.HashMap; import java.util.List; import java.util.Map; import jav ...

  2. IdentityServer4密码模式接入现有用户数据表

    具体接入identityserver请看文档,这里只简单列举部分步骤 1.创建一个web项目,引入Identityserver4的nuget包 2.新建一个类,实现IResourceOwnerPass ...

  3. C# Task.WhenAll

    1.有时候我们需要同时执行一些操作,然后把这些操作的结果进行汇总,以达到用异步处理降低操作耗时的效果,此时我们会考虑使用Task,而Task.WhenAll则排上了用场. public void Is ...

  4. Python考试_第一次

    python基础数据类型考试题 考试时间:两个半小时 满分100分(80分以上包含80分及格) 一,基础题. 1. 简述变量命名规范(3分) 答:(1) 变量为数字,字母以及下划线的任意组合,且不能以 ...

  5. controller 层 date 类型的参数,SpringBoot自动转换 dateformat

    这样客户端提交的String 就可以自动转换为Date了!!!

  6. 二、eureka服务端注册服务

    所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 入口 上文我们说到,eureka是使用jersey来对外提供restful风格的rpc调 ...

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

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

  8. 7.Redis的发布订阅

    Redis消息的订阅/发布 a)是什么 进程间的一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. b)命令 SUBSCRIBE订阅 PUBLISH发布 c)案例 先订阅后发布后才 ...

  9. Templates/Code Combinations

    Queries: Templates/Code Combinations   sql>select * from gl_dynamic_summ_combinations where CODE_ ...

  10. js 定时器(setTimeout/setInterval)出现变量未定义(xxx is not defined) 的解决方法

    首先声明本人资质尚浅,如有错误,欢迎指正.共同提高. ------------------------------------------------------------------------- ...