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 ...
随机推荐
- jenkins里的定时构建
1. 定时构建语法:* * * * * (五颗星,多个时间点,中间用逗号隔开)第一个*表示分钟,取值0~59第二个*表示小时,取值0~23第三个*表示一个月的第几天,取值1~31第四个*表示第几月,取 ...
- (C#)WPF:Margin属性和Padding属性的介绍
1.在进行界面设计时,Margin 和Padding都是对边距进行限制的,其区别在于“一个主外,一个主内”. Margin (边缘)是约束控件与容器控件的边距,设置值分别代表左上右下,使用 Margi ...
- open-falcon监控系统
官方文档 https://book.open-falcon.org/zh/intro/index.html 一.Open-Falcon介绍 1.监控系统,可以从运营级别(基本配置即可),以及应用级别( ...
- nyoj 53-不高兴的小明 (遍历)
53-不高兴的小明 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:28 submit:89 题目描述: 小明又出问题了.妈妈认为聪明的小明应该 ...
- SpringBoot 配置文件与依赖库分离打包配置
一.应用场景 一般情况下我们对springboot应用打包时使用springboot的maven插件spring-boot-maven-plugin的maven进行打包,打包完成得到一个fatjar, ...
- 学习记录:《C++设计模式——李建忠主讲》4.“单一职责”模式
单一职责模式:在软件组件的设计中,如果责任划分的不清晰,使用继承得到的结果往往是随着需求的变化,子类急剧膨胀,同时充斥着重复代码,这时候的关键是划清责任. 典型模式:装饰模式(Decorator).桥 ...
- wait()、notify、notifyAll()的使用
wait().notify.notifyAll()的使用 参考:https://www.jianshu.com/p/25e243850bd2?appinstall=0 一).java 中对象锁的模型 ...
- code migrate
1. 从Git上clone 仓库到本地. git clone --mirror http://gittest:gittest@192.168.1.x/x.git 2. push 到codecommit ...
- 01-python中一切皆对象
python一切皆对象 Python中一切皆对象,在静态语言中,Java也是面向对象编程,Python要比Java的面向对象编程更加彻底.元类编程以及猴子补丁都是用一切皆对象编程出来的. 1.函数和类 ...
- 微信小程序 + thinkjs + mongoDB 实现简单的前后端交互
说明:这段时间跟老师学习了一下mongodb数据库,这次也是第一次搭建后台服务,出了不少差错,特此来复盘一下,非常感谢对我提供帮助的同学~ 一.使用 thinkjs + mongodb 创建后台服务 ...