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. android 项目集成 微信支付

    0.环境 下载 libammsdk.jar 1.需要的常量值 public class Constant { /** 微信中用到的常量值 */ public static final class WX ...

  2. JS OOP -03 JS类的实现

    JS类的实现: a.理解类的实现机制 b.使用prototype对象定义类成员 c.一种JS类的设计模式 a.理解类的实现机制 在JS中可以使用function关键字来定义一个类. 添加类的成员,在函 ...

  3. MySQL修改默认编码 utf8

    修改liunux下MySql默认编码 1安装mysql后,启动服务并登陆,使用status m命令发现mysql的编码并不是 utf8! mysql> status; 2关闭mysql 服务: ...

  4. pickle 和 base64 模块的使用

    pickle pickle模块是python的标准模块,提供了对于python数据的序列化操作,可以将数据转换为bytes类型,其序列化速度比json模块要高. pickle.dumps() 将pyt ...

  5. CentOS 7安装VMware Tools

    1.启动centos,在此虚拟集中点击VMware Workstation中的虚拟机菜单,点击安装VMware Tools,如果已经安装过点击重新安装VMware Tools 2.查看/dev目录下文 ...

  6. linux修改文件系统注册设备

  7. strconv:各种数据类型和字符串之间的相互转换

    介绍 strconv包实现了基本数据类型和其对应字符串之间的相互转换.主要有一下常用函数:Atoi,Itoa,Parse系列,Formart系列,Append系列 string和int之间的转换 这一 ...

  8. python将一个字符串写入文件中的编码问题

    python2将一个字符串写入文件中: 1.如果字符串是str类型 # -*- coding:utf-8 -*- txtFile="今天天气不错" name = "1.t ...

  9. java_数据类型转换

    一.自动转换 目的类型比原来的类型要大,两种数据类型是相互兼容的. byte--->short short--->int char--->int int--->long/dou ...

  10. Maven生命周期——2

    Maven牛人说-Maven生命周期 http://juvenshun.iteye.com/blog/213959 Maven的三套生命周期: Clean Lifecycle 在进行真正的构建之前进行 ...