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

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

#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. Delphi XE2 之 FireMonkey 入门(16) - 滤镜: 实例测试

    窗体上需要 TImage.TOpenDialog 和六个按钮. unit Unit1; interface uses   System.SysUtils, System.Types, System.U ...

  2. python学习笔记(数据类型)

    python数据类型: int 类型 float 小数类型 string 字符串 布尔类型 a = True b = False 1.列表,也称数组或list或array.它的表达方式通过下标或索引或 ...

  3. [Udemy] ES 7 and Elastic Stack - part 1

    Section 1 基本概念: Index(indices) 相当于 关系型数据库的 table, document 相当于关系型数据库的 row,  还有一个type的概念(可以理解为table的s ...

  4. IIS中配置Office COM组件 [dufu图解系列]

    解决问题: 读写office文档目前有2中方法,一种为引用微软的office com组件(下图为Microsoft Office 16.0 Object Library), 另一种为用第三方组件NPO ...

  5. 2019年华南理工大学软件学院ACM集训队选拔赛 Round1

    TIps: 1.所有代码中博主使用了scanf和printf作为输入输出  2.代码中使用了define LL long long 所以在声明变量的时候 LL其实就等价于long long 希望这两点 ...

  6. hibernate的get方法和load方法区别

    读者需注意:Hibernate版本不同,运行机制不太一样,以下是hibernate3.x作为讲解 get方法: Hibernate会确认一下该id对应的数据是否存在,首先在session缓存中查找,然 ...

  7. HDU 5094 题解(状压BFS)

    题面: Maze 题目中文大意: 这个故事发生在“星际迷航”的背景下. “星际争霸”的副队长史波克落入克林贡的诡计中,被关押在他们的母亲星球Qo’noS上. 企业的上尉詹姆斯·T·柯克(James T ...

  8. React Hooks实现异步请求实例—useReducer、useContext和useEffect代替Redux方案

    本文是学习了2018年新鲜出炉的React Hooks提案之后,针对异步请求数据写的一个案例.注意,本文假设了:1.你已经初步了解hooks的含义了,如果不了解还请移步官方文档.(其实有过翻译的想法, ...

  9. centos解决Could not find a version that satisfies the requirement pip3 (from versions: none)及No matching distribution found for pip3问题

    python环境:python 3.8 报错信息: WARNING: pip is configured with locations that require TLS/SSL, however th ...

  10. neo4j 的cql 语句,增、删、改、查(条件查询)(持续更新)

    前言 因为做一个比赛的项目 ,需要用到 neo4j 数据库,所以要学习其语言cql,特来整理一下他的基本语言. 整片的语句是按照 了 Neo4j 数据库自带的示例 Movie Graph 来写的. 直 ...