CF83A Magical Array 题解
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 题解的更多相关文章
- [LeetCode]Remove Duplicates from Sorted Array题解
Remove Duplicates from Sorted Array: Given a sorted array, remove the duplicates in place such that ...
- Leetcode Find Minimum in Rotated Sorted Array 题解
Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...
- CF1514A Perfectly Imperfect Array 题解
Content 给定一个长度为 \(n\) 的序列,问是否存在一个非空子序列,使得这个子序列所有元素的积不是完全平方数. 数据范围:\(t\) 组数据,\(1\leqslant t\leqslant ...
- CF1007A Reorder the Array 题解
To CF 这道题是排序贪心,将原序列排序后统计答案即可. 但是直接统计会超时,因为排序后具有单调性,所以可以进行一点优化,这样,便可以通过此题. 而这道题的优化在于单调性,因为 \(a[i+1]\) ...
- LeetCode Find Minimum in Rotated Sorted Array
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...
- Search in Rotated Sorted Array II leetcode java
题目: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would ...
- 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 ...
- 【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 ...
- [Leetcode Week11]Kth Largest Element in an Array
Kth Largest Element in an Array 题解 题目来源:https://leetcode.com/problems/kth-largest-element-in-an-arra ...
随机推荐
- mybatis避免sql的like注入
<select id="NotInByEvalQuestion" resultType="com.rm.eval.entity.EvalQnQuestion&quo ...
- jupyter 远程访问
Jupyter 远程访问 jupyter 远程访问的工作方法是,在本地通过浏览器打开jupyter,但是代码和服务运行在远程集群中. 集群设置 首先需要确保集群中安装有python和jupyter. ...
- REPuter注释叶绿体重复序列
REPuter可注释叶绿体重复序列,包括4种类型,Forward(F), Reverse (R), Complement (C), Palindromic (P). REPuter 是可在线注释, 详 ...
- 【shell】真正解决syntax error:unexpected end of file?
今天写了个较长的shell脚本,结构嵌套比较多,最后运行时,出现了syntax error: unexpected end of file的错误. 这个之前碰到过,经常在win系统转移脚本文件到uni ...
- 【Perl示例】整合多个文件
这个需求是在生信分析中几乎天天用到,各种语言都能实现,也都各有特点.这次以perl为例. 已知 文件CT-VS-CON.All.xls为全部蛋白表达矩阵及其差异分析结果. 文件Homo_sapiens ...
- android Fragment跳转Fragment
android Fragment跳转Fragment,最新的android studio3 在系统模板建立的BottomNavigationView 中跳转方式 此版本下不能用FragmentMana ...
- JavaBean内省与BeanInfo
Java的BeanInfo在工作中并不怎么用到,我也是在学习spring源码的时候,发现SpringBoot启动时候会设置一个属叫"spring.beaninfo.ignore", ...
- 大数据学习day34---spark14------1 redis的事务(pipeline)测试 ,2. 利用redis的pipeline实现数据统计的exactlyonce ,3 SparkStreaming中数据写入Hbase实现ExactlyOnce, 4.Spark StandAlone的执行模式,5 spark on yarn
1 redis的事务(pipeline)测试 Redis本身对数据进行操作,单条命令是原子性的,但事务不保证原子性,且没有回滚.事务中任何命令执行失败,其余的命令仍会被执行,将Redis的多个操作放到 ...
- 容器之分类与各种测试(四)——multimap
multiset和multimap的具体区别在于,前者的key值就是自己存储的value,后者的key与value是分开的不相关的. 例程 #include<stdexcept> #inc ...
- javaAPI1
Iterable<T>接口, Iterator<T> iterator() Collection<E>:接口,add(E e) ,size() , Object[] ...