B. Uniqueness 删除最小区间内的元素使得剩余元素唯一
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.
题意:删除一段连续的区间,使得剩下的所有元素都是唯一的,求满足要求的最小区间长度
题解:逆向考虑,从左边开始[1,i ]最多可以取x个不同的数,然后从n到 i+1 最多可以连续取y个不同的数,不断枚举 i ,取cnt=max( cnt,x+y) 的最大值,n - cnt就是可以删除的最小长度
#include<iostream>
#include<algorithm>
#include<math.h>
#include<map>
using namespace std;
int a[];
map<int,int>mp;
int main()
{
int n,ans=;
cin>>n;
for(int i=;i<=n;i++)
cin>>a[i];
for(int i=;i<=n;i++)
{
mp.clear();
int cnt=;
for(int j=;j<=i;j++)
{
if(mp[a[j]]==)
{
mp[a[j]]=;
cnt++;
}
else
break;
} for(int j=n;j>i;j--)
{
if(mp[a[j]]==)
{
mp[a[j]]=;
cnt++;
}
else
break;
}
ans=max(ans,cnt);
}
cout<<n-ans<<endl;
return ;
}
B. Uniqueness 删除最小区间内的元素使得剩余元素唯一的更多相关文章
- 求包含每个有序数组(共k个)至少一个元素的最小区间
title: 求包含每个有序数组(共k个)至少一个元素的最小区间 toc: false date: 2018-09-22 21:03:22 categories: OJ tags: 归并 给定k个有序 ...
- YTU 2986: 删除区间内的元素(线性表)
2986: 删除区间内的元素(线性表) 时间限制: 1 Sec 内存限制: 2 MB 提交: 8 解决: 3 题目描述 若一个线性表L采用顺序存储结构,其中元素都为整数.设计一个算法,删除元素值在 ...
- LOJ #6279. 数列分块入门 3-分块(区间加法、查询区间内小于某个值x的前驱(比其小的最大元素))
#6279. 数列分块入门 3 内存限制:256 MiB时间限制:1500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论 3 题目描述 给 ...
- LOJ #6278. 数列分块入门 2-分块(区间加法、查询区间内小于某个值x的元素个数)
#6278. 数列分块入门 2 内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论 6 题目描述 给出 ...
- HDU 3264 区间内的最大最小之差
题目链接:http://poj.org/problem?id=3264 题目大意:在给定一堆牛的数量以及其高度的时候,每次给定一段区间,求这个区间内最高的牛和最矮的牛的高度之差为多少. 可以直接利用R ...
- 转:最小区间:k个有序的数组,找到最小区间使k个数组中每个数组至少有一个数在区间中
转:http://www.itmian4.com/thread-6504-1-1.html 最小区间原题 k个有序的数组,找到最小的区间范围使得这k个数组中,每个数组至少有一个数字在这个区间范围内.比 ...
- hdu3437 划分树 区间内小于第K大的值得和
Minimum Sum Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- CSS样式设置语法全解,样式优先级、值和单位、字体、文本、块级元素,行内元素,替换元素、非替换元素、display、float、position、table、li、光标、边距边框、轮廓、颜色背景
全栈工程师开发手册 (作者:栾鹏) 一个demo学会css css选择器全解 css操作语法全解 CSS样式设置语法全解: 样式优先级 1. !important标记的样式 > 内联样式(sty ...
- 单调队列——求m区间内的最小值
单调队列,顾名思义是指队列内的元素是有序的,队头为当前的最大值(单调递减队列)或最小值(单调递增序列),以单调递减队列为例来看队列的入队和出队操作: 1.入队: 如果当前元素要进队,把当前元素和队尾元 ...
随机推荐
- 安卓之文本视图TextView及跑马灯效果
一.基本属性和设置方法 二.跑马灯用到的属性与方法说明 三.省略方式的取值说明 四.跑马灯效果案例代码 (1)布局xml文件 <?xml version="1.0" en ...
- 2.1.FastDFS-单机拆分版-调度器安装配置
Centos610系列配置 我们在Centos610FastDFS单机模式-FastDFS安装 中已经完成了FastDFS的安装,接下来我们进行FastDFS调度器的安装. 1.找到FastDFS配置 ...
- 为什么局部内部类和匿名内部类只能访问 final 的局部变量?
首先,我们看一个局部内部类的例子: class OutClass { private int age = 12; public void outPrint(final int x) { class I ...
- ASP.NET Core搭建多层网站架构【8.3-编写角色业务的增删改】
2020/01/29, ASP.NET Core 3.1, VS2019 摘要:基于ASP.NET Core 3.1 WebApi搭建后端多层网站架构[8.3-编写角色业务的增删改] 编写最简单的增删 ...
- spring web 测试用例
spring web 测试有三种方式 1. 自己初始化 MockMvc 2.依赖@springbootTest 它会帮你生产 webTestClient ,只需自己注入即可. 3.启动的时候,不加载整 ...
- 【代码审计】VAuditDemo 后台登录功能验证码绕过
在 admin/logCheck.php中 $_POST['user']和$_POST['pass'] 未经过任何过滤或者编码处理就传入到$query中,可能存在万能密码绕过机制 但是$pass经过了 ...
- 源代码管理工具(1)——SVN(1)——SVN 的使用新手指南,具体到步骤详细介绍----TortoiseSVN
一.客户端的使用: TortoiseSVN(下载地址:https://pan.baidu.com/s/14cAEV5ZfMA9mLlQAb4oznw 这里有包含中文版的语言包). 1.先下载安装(这个 ...
- 实现简单HttpServer案例
<html> <head> <title>第一个表单</title> </head> <body> <pre> me ...
- html 中js 如何给字符串加换行符
var str = 你好'+"\n"+ '世界'; 这种写法在html中是会被识别为"你好\n世界" 那么如何保证其这么写会被识别,只需要在该div的样式中加入 ...
- java集合知识点
若不重写equals方法,则调用的是object对象的equals方法,相当于==比较,比较的是对象的内存地址 |------Collection接口:单列集合,用来存储一个一个对象 |------L ...