B. Uniqueness(尺取)
2 seconds
256 megabytes
standard input
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.
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.
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.
3
1 2 3
0
4
1 1 2 2
2
5
1 4 1 4 9
2
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(尺取)的更多相关文章
- Gym 100703I---Endeavor for perfection(尺取)
题目链接 http://codeforces.com/problemset/gymProblem/100703/I Description standard input/outputStatement ...
- 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 ...
- hdu 4123 Bob’s Race 树的直径+rmq+尺取
Bob’s Race Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- Codeforces Round #116 (Div. 2, ACM-ICPC Rules) E. Cubes (尺取)
题目链接:http://codeforces.com/problemset/problem/180/E 给你n个数,每个数代表一种颜色,给你1到m的m种颜色.最多可以删k个数,问你最长连续相同颜色的序 ...
- poj2566尺取变形
Signals of most probably extra-terrestrial origin have been received and digitalized by The Aeronaut ...
- poj2100还是尺取
King George has recently decided that he would like to have a new design for the royal graveyard. Th ...
- hdu 6231 -- K-th Number(二分+尺取)
题目链接 Problem Description Alice are given an array A[1..N] with N numbers. Now Alice want to build an ...
- Codeforces 939E Maximize! (三分 || 尺取)
<题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-me ...
- cf1121d 尺取
尺取,写起来有点麻烦 枚举左端点,然后找到右端点,,使得区间[l,r]里各种颜色花朵的数量满足b数组中各种花朵的数量,然后再judge区间[l,r]截取出后能否可以供剩下的n-1个人做花环 /* 给定 ...
- HDU 5178 pairs【二分】||【尺取】
<题目链接> 题目大意: 给定一个整数序列,求出绝对值小于等于k的有序对个数. 解题分析: $O(nlong(n))$的二分很好写,这里就不解释了.本题尺取$O(n)$也能做,并且效率很不 ...
随机推荐
- JDBC 复习3 存取Oracle大数据 clob blob
1 目录结构记得导包咯 mysql oracle 2 代码,DBUtil工具类见前面的随笔博文 package dbex.mysql; import java.io.BufferedReader; i ...
- 恺撒密码 B
恺撒密码 B ...
- es6中Array.from()
Array.from()方法就是将一个类数组对象或者可遍历对象转换成一个真正的数组. 那么什么是类数组对象呢?所谓类数组对象,最基本的要求就是具有length属性的对象. 1.将类数组对象转换为真正数 ...
- 2 webpack 4 加vue搭建开发环境最终配置
1 package.json { "name": "c", "version": "1.0.0", "desc ...
- stm32 窗口看门狗 WWDG
窗口看门狗WWDG其实和独立看门狗类似,它是一个7位递减计数器不断的往下递减计数,当减到一个固定值0x40时还不喂狗的话,产生一个MCU复位,这个值叫窗口的下限,是固定的值,不能改变 窗口看门狗(WW ...
- window service 批处理安装/卸载命令
开启服务 @echo.服务启动...... @echo off @sc create 服务名 binPath= "C:\Users\Administrator\Desktop\win32sr ...
- macro 标签,和静态文件,以及templates
1 模板 -Templates 1 什么是模板 模板,在Flask中就是允许响应给用户看的网页在模板中,允许包含“占位变量”来表示动态的内容 模板最终也会被解析成字符串再响应给客户端,这一过程通常称为 ...
- Metasploit Penetration (第一夜加班)
1.最近白天瞒着上班,晚自习看英语,还要瞒着写论文(现在是看,之前的部分章节被老师否定了,现在开始要从新进行整理)所以只有这晚上来开始看我的渗透测试了,发现渗透的很多知识,只要你平时不用很快就会生疏, ...
- python 爬虫 Selenium的简单使用
一.Selenium基础介绍及安装 1.Selenium简介 Selenium是一个用于测试网站的自动化测试工具,支持各种浏览器包括Chrome.Firefox.Safari等主流界面浏览器,同时也支 ...
- Django drf:认证及组件、token、局部钩子源码分析
一.drf认证功能 二.token讲解 三.局部钩子源码分析 一.drf认证功能 1.认证简介: 只有认证通过的用户才能访问指定的url地址,比如:查询课程信息,需要登录之后才能查看,没有登录则不能查 ...