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 ...
随机推荐
- getClass()和instanceof以及类的equals方法
在比较两个类时,常见有两种做法,一种是x.getClass() == y; 一种是x instanceof y,下面我们来比较这两种做法的区别. getClass()返回一个对象所属的类 public ...
- 用这个库 3 分钟实现让你满意的表格功能:Bootstrap-Table
本文作者:HelloGitHub-kalifun 这是 HelloGitHub 推出的<讲解开源项目>系列,今天给大家推荐一个基于 Bootstrap 和 jQuery 的表格插件:Boo ...
- hdu 1068 Girls and Boys (最大独立集)
Girls and BoysTime Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 目录(cd mkdir rmdir rm pwd ls) 文件(ln touch mv rm cat more head rail) 文件权限(chmod chown chgrp) 文件通配符(* ? [])
记住Linux目录树的结构是一个称职Linux系统管理员的必备素质! 目录漫游cd cd - 目录显示pwd 目录管理 mkdir -p a/b/c/1 parent创建多层目录 -m 700 ...
- boost.asio新框架的设计概念总结
1.66版本,boost.asio库重新设计了框架,目前最新版为1.71.读了几天代码后,对框架中相关概念总结.因为是泛型编程的库,所以分析的概念层的设计. 可通过boost官方文档,strand的1 ...
- Win32 COM组件 x Android Service (二)
继续上一篇. 如果不使用AIDL(Android Interface Definition Language接口描述语言)编写服务接口的话,(COM组件,CORBA组件,ICE组件以及其它远程调用框架 ...
- React-基础总结
使用1. // js文件,第一部引入React(大写,不然保错) import React from 'react' // 创建数组 const arrList = Array.from({lengt ...
- jquery ajax提交数据给后端
大家好,今天铁柱兄给大家带一段jquery ajax提交数据给后端的教学. 初学javaweb的同学前端提交数据基本上都是用form表单提交,这玩意儿反正我是觉得不太好玩.而JavaScript aj ...
- html学习笔记--xdd
<!DOCTYPE html> <html> <head> <title>HTML学习笔记</title> <meta charset ...
- 使用three.js创建大小不随着场景变化的文字
使用three.js创建大小不随着场景变化的文字,需要以下两步: 1.将文字绘制到画布上. 2.创建着色器材质,把文字放到三维场景中. 优点: 1.跟用html实现文字相比,这些文字可以被模型遮挡,更 ...