C.Dominated Subarray
题目:受主导的子序列##
题意:序列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的更多相关文章
- Educational Codeforces Round 76 (Rated for Div. 2) C. Dominated Subarray 水题
C. Dominated Subarray Let's call an array
- 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 ...
- 【CF1257C】Dominated Subarray【贪心】
题意:给定一个数组,求最小的字数组使得数组里存在至少一对重复元素 题解:每个点求出他的后继在哪,然后每次贪心就这个点到他的后继为一个子数组,求出最小的就是答案 #include<iostream ...
- Educational Codeforces Round 76 (Rated for Div. 2)
传送门 A. Two Rival Students 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/13 22:37:26 */ #incl ...
- CF Educational Round 78 (Div2)题解报告A~E
CF Educational Round 78 (Div2)题解报告A~E A:Two Rival Students 依题意模拟即可 #include<bits/stdc++.h> us ...
- [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 ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- 监听器以及在监听类里面获得bean的方法
1实现HttpSessionListener和ServletContextListener,2个接口 2然后在contextInitialized初始化方法里面: ServletContext app ...
- ios沙盒机制---基本数据类型的存取和文件的基本操作
沙盒快速存储及读取 存储: [[NSUserDefaults standardUserDefaults] setObject:@"abc" forKey:@"1&quo ...
- Centos 6/7 常用命令总结 (基础)
Centos 6/7 常用命令总结 (基础): 参考链接:https://www.cnblogs.com/linhaifeng/p/6045600.html 目录介绍: a) bin目录:用来存放常用 ...
- [LC] 700题 Search in a Binary Search Tree (二叉搜索树中的搜索) (二叉搜索树)
①中文题目 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. 例如, 给定二叉搜索树: 在上述示例 ...
- 在C\C++中char 、short 、int各占多少个字节
在C\C++中char .short .int各占多少个字节 : #include <bits/stdc++.h> using namespace std; int main() { co ...
- SpringBoot 源码解析 (七)----- Spring Boot的核心能力 - 自定义Servlet、Filter、Listener是如何注册到Tomcat容器中的?(SpringBoot实现SpringMvc的原理)
上一篇我们讲了SpringBoot中Tomcat的启动过程,本篇我们接着讲在SpringBoot中如何向Tomcat中添加Servlet.Filter.Listener 自定义Servlet.Filt ...
- react-router重定向
① ②通过this.props重定向
- go modules 学习
go modules 学习 tags:golang 安装 只需要golang的版本是1.11及之后的,这个模块就内置好了 环境变量 (1) 配置GoLang的GOROOT (2) 可以不配置GoLan ...
- 2019-10-24:伪静态,VULHUB搭建靶场,宽字节sql注入,笔记
伪静态1,需要开启站点的重写机制,需要修改配httpd配置文件,将LoadModule rewrite_module modules/mod_rewrite.so注释取消,需要apache支持解析.h ...
- python 实现图片批量加入水印!pillow 入门实战!
写文章的时候可以设置是否添加水印.可是,有些图片可能想加水印,有些不想加水印,该怎么办呢? 配置环境 python3 + pillow pip3 install pillow 引入库 from PIL ...