题目:受主导的子序列##

题意:序列t至少有2个元素,我们称序列t被数字出现次数最多的元素v主导,且出现次数最多的元素必须是唯一的

你被给予了序列a1, a2, ..., an,计算它的最短受主导子序列,或者说这里没有这种序列

[4, 1, 2, 4, 5, 4, 3, 2, 1]的最短受主导子序列为[4, 5, 4]

链接:(受主导的子序列)[https://codeforces.com/contest/1257/problem/C]

分析:我们可以检查每个相同数字(受主导的元素)的对,求出它们之间的距离,然后在每对距离里取最小值

但是可以有更好的方法,时间复杂度为O(n),我们可以从头开始检查元素,如果出现相同的元素,就求出它们之间的距离,然后将后面的元素作为下一段开始受主导子序列的开头。

证明:每段受主导子序列是两两分明,且里面没有两端相同的元素,否则则会产生更短的受主导子序列

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <algorithm> using namespace std; const int N = 2e5 + 5;
const int INF = 0x3f3f3f3f;
vector<int> a;
void solve()
{
int n;
cin >> n;
a.resize(n + 1); for (int i = 0; i < n; ++i)
cin >> a[i];
if (n == 1)
{
cout << -1 << endl;
return;
}
int ans = INF;
vector<int> lst(n + 1, -1);//n + 1个 -1 for (int i = 0; i < n; i++) {
if (lst[a[i]] != -1)
ans = min(ans, i - lst[a[i]] + 1);
lst[a[i]] = i;
}
if (ans > n)
ans = -1;
cout << ans << endl;
a.clear();
lst.clear();
} int main()
{
int T;
cin >> T;
for (int i = 1; i <= T; ++i)
{
solve();
} return 0;
}

C.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. 【CF1257C】Dominated Subarray【贪心】

    题意:给定一个数组,求最小的字数组使得数组里存在至少一对重复元素 题解:每个点求出他的后继在哪,然后每次贪心就这个点到他的后继为一个子数组,求出最小的就是答案 #include<iostream ...

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

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

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

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

  6. [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  7. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  8. [LeetCode] Maximum Product Subarray 求最大子数组乘积

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  9. [LeetCode] Maximum Subarray 最大子数组

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

随机推荐

  1. 部署django

    添加uwagi配置文件 在你项目的根目录中创建mysite.xml(名字无所谓),或者创建mysite.ini,输入以下内容: <uwsgi> <socket>127.0.0. ...

  2. python06-列表表达式、生成器表达式及其面试题、解耦简单介绍、函数递归相关

    目录: 一.列表推导式 二.生成器表达式 三.集合生成器 四.生成器面试题 五.解耦简单介绍 六.函数递归相关 一.列表推导式 需求:将[1,3,5]中的每个元素平方 正常思路: new_list = ...

  3. 做HTML静态页面时遇到的问题总结

    1. 如果所示,问题:“首页”和“闲置”文字部分位于table中部 解决方法:需要取消vertical-align:middle属性,将其设置为vertical-align:top,并将文本的高度改为 ...

  4. 破解网站二维码验证,Java实现,不调用任何平台api接口

    package image.images; import java.io.File; import java.io.IOException; import java.io.InputStream; i ...

  5. ubuntu 16.04上源码编译opengv | compile opengv on ubuntu 16.04

    本文首发于个人博客https://kezunlin.me/post/1e5d14ee/,欢迎阅读! compile opengv on ubuntu 16.04 Series compile open ...

  6. 2019-9-25:渗透测试,基础学习,medusa爆破学习

    Medusa(美杜莎)暴力破解使用 该文章仅供学习,利用方法来自网络文章,仅供参考 一.简介 medusa(美杜莎)是一个速度快,支持大规模并行,模块化,爆破登陆,可以同时对多个主机,用户或是密码执行 ...

  7. 解析深度学习 语音识别实践 pdf下载

    链接:https://pan.baidu.com/s/1jd8_2nbz6M9e20lI3JdVGA  密码:1ikc 我从别人那里买的!可以友情赞助资瓷!

  8. Python编译升级

    [root@localhost python]# tar xvf Python-3.6.9.tgz [root@localhost python]# cd Python-3.6.9/ [root@lo ...

  9. DOM属性

    节点属性: 文档里的每个节点都有以下属性. nodeName nodeName属性将返回一个字符串,其内容是给定节点的名字: name = node.nodeName 如果给定节点是一个属性节点,no ...

  10. Hadoop streaming脚本中约束关系参数详解

    1 -D mapred.output.key.comparator.class=org.apache.hadoop.mapred.lib.KeyFieldBasedComparator \ 2 -D ...