题意:给定一个数组,求最小的字数组使得数组里存在至少一对重复元素

题解:每个点求出他的后继在哪,然后每次贪心就这个点到他的后继为一个子数组,求出最小的就是答案

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#define ll long long
using namespace std;
int T,n,ans;
int a[],nxt[],hd[];
int main()
{
scanf("%d",&T);
while(T--)
{
for(int i=;i<=n;i++)hd[i]=nxt[i]=;
scanf("%d",&n);
for(int i=;i<=n;i++){scanf("%d",&a[i]);nxt[i]=hd[a[i]];hd[a[i]]=i;}
ans=1e9;
for(int i=;i<=n;i++)if(nxt[i])ans=min(ans,i-nxt[i]+);
printf(ans==1e9?"-1\n":"%d\n",ans);
}
return ;
}

【CF1257C】Dominated Subarray【贪心】的更多相关文章

  1. Educational Codeforces Round 76 (Rated for Div. 2) C. Dominated Subarray 水题

    C. Dominated Subarray Let's call an array

  2. Educational Codeforces Round 76 (Rated for Div. 2) C. Dominated Subarray

    Let's call an array tt dominated by value vv in the next situation. At first, array tt should have a ...

  3. C.Dominated Subarray

    题目:受主导的子序列 题意:序列t至少有2个元素,我们称序列t被数字出现次数最多的元素v主导,且出现次数最多的元素必须是唯一的 你被给予了序列a1, a2, ..., an,计算它的最短受主导子序列, ...

  4. CF Educational Round 78 (Div2)题解报告A~E

    CF Educational Round 78 (Div2)题解报告A~E A:Two Rival Students​ 依题意模拟即可 #include<bits/stdc++.h> us ...

  5. Educational Codeforces Round 76 (Rated for Div. 2)

    传送门 A. Two Rival Students 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/13 22:37:26 */ #incl ...

  6. LeetCode--Best Time to Buy and Sell Stock (贪心策略 or 动态规划)

    Best Time to Buy and Sell Stock Total Accepted: 14044 Total Submissions: 45572My Submissions Say you ...

  7. Codeforces 754A Lesha and array splitting(简单贪心)

    A. Lesha and array splitting time limit per test:2 seconds memory limit per test:256 megabytes input ...

  8. [LeetCode]题53:Maximum Subarray

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  9. 152. Maximum Product Subarray最大乘积子数组/是否连续

    [抄题]: Given an integer array nums, find the contiguous subarray within an array (containing at least ...

随机推荐

  1. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_07 缓冲流_3_BufferedInputStream_字节缓冲

    内容改成abc 来个数组缓冲

  2. 读取资源中的GIF文件相应像素宽高度

    代码参考了如下网页的实现: https://www.cnblogs.com/zy791976083/p/9921069.html 整理成一个函数: BOOL GetResGifSize(long nR ...

  3. 一些输出、处理细节&注意点

    https://blog.csdn.net/qq_41071646/article/details/79953476 输出百分比的时候,结果需要加上一个EPS(1e-6)四舍五入保证精度. 卡精度—— ...

  4. Vue 基础 day02

    Vue Devtools 安装 https://chrome.google.com/webstore/search/vue%20devtools?hl=zh-CN 需要翻墙 过滤器 概念: Vue.j ...

  5. [Git] 004 初识 Git 与 GitHub 之查看历史记录

    在 GitHub 的 UI 界面使用 Git 查看历史纪录 1. 右侧有个 history 2. 点击后跳转页面 3. 点击相应标题(commit 时写的),进入相应版本(历史) 4. 我选择了 I ...

  6. 【转载】研发应该懂的binlog知识(下)

    引言 这篇是<研发应该懂的binlog知识(上)>的下半部分.在本文,我会阐述一下binlog的结构,以及如何使用java来解析binlog.不过,话说回来,其实严格意义上来说,研发应该还 ...

  7. luogu 3426题解 (KMP)

    题面 Byteasar 想在墙上涂一段很长的字符,他为了做这件事从字符的前面一段中截取了一段作为模版. 然后将模版重复喷涂到相应的位置后就得到了他想要的字符序列.一个字符可以被喷涂很多次,但是一个位置 ...

  8. HDU-2189来生一起走

    题目: 今天,又来了n位志愿者,指挥部需要将他们分为若干个小组,小组的数量不限,但是要求每个小组的人数必须为素数,请问我们有几种分组的方法呢? 特别说明: 1.可以只有一个组: 2.分组的方法只和人数 ...

  9. SCUT - 37 - 手速帝CZK - 分块

    https://scut.online/p/37 一开始想到要根号分块预处理,但是不太懂具体怎么写.想不到如此暴力. #include<bits/stdc++.h> using names ...

  10. .NET的优点(转载)

    一:什么是.NET?它包括什么? .Net是为简化在第三代因特网的高分布式环境下的应用程序开发,基于开放互联网标准和协议之上,实现异质语言和平台高度交互性,而构建的新一代计算和通信平台. .Net主要 ...