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.入队: 如果当前元素要进队,把当前元素和队尾元 ...
随机推荐
- java基础数据类型和处理
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSON; import java.io.*; import j ...
- idea垂直分屏
1.找到分屏功能 搜索keymap(注意大小写): 2.Split Vertically 垂直分屏/Split Horizontally 水平分屏 3.添加快捷方式
- 模块学习-shutil
高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length]) 将文件内容拷贝到另一个文件中,可以部分内容 shutil.copyfile(s ...
- 201771010135杨蓉庆《面向对象程序设计(java)》第四周学习总结
学习目标 1.掌握类与对象的基础概念,理解类与对象的关系: 2.掌握对象与对象变量的关系: 3.掌握预定义类的基本使用方法,熟悉Math类.String类.math类.Scanner类.LocalDa ...
- dfs & bfs总结
dfs 最简单的三种形式递归总结 bfs 百度https://baike.baidu.com/item/%E5%AE%BD%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7 ...
- node.js express 中文参考手册
https://www.runoob.com/w3cnote/express-4-x-api.html 原文地址:https://www.zybuluo.com/bajian/note/444152 ...
- [idea] 解决 idea 复制进项目的文件运行时无法找到的问题
解决方法一: Rebuild后,重启项目 解决方法二:
- 转载--php函数使用--var_export
var_export用于将数组转换成字符串 <?php $arr = [ 'key1'=>'val1', 'key2'=>'val2', 'key3'=>'val3', 'ke ...
- Centos7 [ubuntu] 安装pycharm2019.1.3并永久破解教程
一.安装pycharm2019专业版并激活步骤 1.拉取安装包 # wget https://download.jetbrains.com/python/pycharm-professional- ...
- Java中的基本数据类型语法补充
变量要先赋值后使用 不给变量赋值代表什么 不赋值就使用会怎样 (会报错) 计算并赋值运算符 作用是为了让代码更加简洁.比如 a = a + 10,可以简化为 a+=10 += -= *= /= %= ...