Content

有一个长度为 \(n\) 的序列 \(a_1,a_2,a_3,...,a_n\)。定义一个“神奇数组”为在上面的序列中最大值和最小值相等的子序列。求出这个序列中“神奇数组”的个数。

数据范围:\(1\leqslant n\leqslant 10^5,0\leqslant|a_i|\leqslant 10^9\)。

Solution

这个题目直接模拟肯定会爆炸,所以我们考虑一个更高效率的算法。

首先,我们明显知道,最小值与最大值都相等的序列就是所有元素都相等的序列。

我们先通过样例来找找规律:

  • 长度为 \(4\) 的序列 \(\{2,1,1,4\}\) 的答案是 \(5=1\times(1+2)\div2+2\times(2+3)\div2+1\times(1+2)\div2\)。
  • 长度为 \(5\) 的序列 \(\{-2,-2,-2,0,1\}\) 的答案是 \(8=3\times(3+1)\div2+2\times1\times(1+2)\div2\)。

是不是发现规律了?

我们可以归纳出来:如果有一个长度为 \(k\),且最小值与最大值都相等的序列,那么这里面的“神奇数组”有 \(\dfrac{k(k+1)}{2}\) 个。

那么,这道题目很明显就出来了。

注意这里数据范围大,要开 \(\texttt{long long}\)。

Code

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std; int n, a[100007];
long long ans, cnt = 1; int main() {
scanf("%d%d", &n, &a[1]);
for(int i = 2; i <= n; ++i) {
scanf("%d", &a[i]);
if(a[i] == a[i - 1]) cnt++;
else {
// printf("%d\n", cnt * (cnt + 1) / 2);
ans += cnt * (cnt + 1) / 2;
cnt = 1;
}
}
ans += cnt * (cnt + 1) / 2;
printf("%lld", ans);
}

CF83A Magical Array 题解的更多相关文章

  1. [LeetCode]Remove Duplicates from Sorted Array题解

    Remove Duplicates from Sorted Array: Given a sorted array, remove the duplicates in place such that ...

  2. Leetcode Find Minimum in Rotated Sorted Array 题解

    Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...

  3. CF1514A Perfectly Imperfect Array 题解

    Content 给定一个长度为 \(n\) 的序列,问是否存在一个非空子序列,使得这个子序列所有元素的积不是完全平方数. 数据范围:\(t\) 组数据,\(1\leqslant t\leqslant ...

  4. CF1007A Reorder the Array 题解

    To CF 这道题是排序贪心,将原序列排序后统计答案即可. 但是直接统计会超时,因为排序后具有单调性,所以可以进行一点优化,这样,便可以通过此题. 而这道题的优化在于单调性,因为 \(a[i+1]\) ...

  5. LeetCode Find Minimum in Rotated Sorted Array

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...

  6. Search in Rotated Sorted Array II leetcode java

    题目: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would ...

  7. Search in Rotated Sorted Array leetcode java

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...

  8. 【leetcode刷题笔记】Find Minimum in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  9. [Leetcode Week11]Kth Largest Element in an Array

    Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...

随机推荐

  1. [cf1184E]Daleks' Invasion

    先求出任意一棵最小生成树,然后对边分类讨论1.非树边,答案即最小生成树的环上的最长边2.树边,反过来考虑,相当于对于每一个点对那条路经打上标记,取min对于1直接用倍增维护即可,对于2可以用树链剖分/ ...

  2. 阿里云服务器的MySQL连接和vscode远程连接

    目录 一.前言 二.使用Navicat等软件连接MySQL 1. 修改服务器系统密码 2. 防火墙选项添加MySQL 3. 使用Navicat连接 三.使用vscode连接服务器 一.前言 双十一的时 ...

  3. Cortex-A系列中断

    1. 回顾STM32系统 1.1 中断向量表 ARM芯片冲0x00000000,在程序开始的地方存放中断向量表,按下中断时,就相当于告诉CPU进入的函数.描述很多个中断服务函数的表. 对于STM32来 ...

  4. Electron快速入门

    node -v npm -v 安装node环境 my-electron-app/ ├── package.json ├── main.js └── index.html 为您的项目创建一个文件夹并安装 ...

  5. 【Azure API 管理】在APIM 中添加 log-to-eventhub 策略,把 Request Body 信息全部记录在Event Hub中

    问题描述 根据文档 https://docs.azure.cn/zh-cn/api-management/api-management-howto-log-event-hubs, 可以将Azure A ...

  6. Unique Path AGC 038 D

    Unique Path AGC 038 D 考虑如果两个点之间只能有一个边它们就把它们缩起来,那么最后缩起来的每一块都只能是一棵树. 如果两个点之间必须不止一个边,并且在一个连通块,显然无解. 首先把 ...

  7. python12类

    self 表示类里面的对象的引用 python一般不需要去解决内存管理,解释器会进行自动给回收 #类鱼类之间空格两行,前面的函数里面也是两行,类里面的方法个一行 class Cat(object): ...

  8. Shell中 ##%% 操作变量名

    在linxu平台下少不了对变量名的处理,今天记录下shell中 ##%% 对变量名的操作. #操作左侧,%操作右侧. #号处理方式: 对于单个#,处理对象为变量中指定的第一个符号左侧字符串, 对于两个 ...

  9. Excel-给出指定数值的日期 date()

    DATE函数 函数名称:DATE 主要功能:给出指定数值的日期. 使用格式:DATE(year,month,day) 参数说明:year为指定的年份数值(小于9999):month为指定的月份数值(可 ...

  10. 『学了就忘』Linux文件系统管理 — 62、手动分配swap分区

    目录 1.查看swap分区情况 2.手动修改swap分区 3.格式化swap分区 4.使用swap分区 5.配置swap分区开机之后自动挂载 1.查看swap分区情况 swap分区就相当于是内存的一个 ...