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 ...
随机推荐
- Hardware assisted virtualization and data execution protection must be enabled in the BIOS. See https://docs.docker.com/docker-for-windows/troubleshoot/#virtualization
解决办法:先关闭 Hyper-V ,然后重新开启 Hyper-V 即可! 来自:https://zhuanlan.zhihu.com/p/51939654
- redis实现最简单的锁
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifact ...
- 互联网java面试宝典
1.为什么使用消息队列啊? 答题: 消息队列的核心功能就是:解耦合,异步,流量削峰解耦:接口调用发送,那如果E系统也要这个数据呢?那如果C系统现在不需要了呢?现在A系统又要发送第二种数据了呢?A系统负 ...
- [SVN] Branch and Tag
在 SVN 中,如何建立分支以及如何标记Tag. 右键要处理的文件夹,选择 "TortoiseSVN" - "Branch/tag...",进入下面界面: To ...
- uniapp中vuex的基本使用
1. 创建一个uniapp项目 2. 在项目目录下用npm安装 vuex npm install vuex --save 3. 在项目根目录下创建 store文件夹,在store文件夹中创建 inde ...
- NFLSOJ #10317. -「2020联考北附2」三千世界(找等价表达+树形 dp)
题面传送门 出题人可能原本感觉没啥难度的 T2 竟然变成了防 AK 题,奇迹奇迹( 首先带着这个 \(\max\) 肯定不太好处理,考虑找出 \(f(S)\) 的等价表达.我们考虑以 \(1\) 为根 ...
- Codeforces 690A2 - Collective Mindsets (medium)
Codeforces 题面传送门 & 洛谷题面传送门 一道脑筋急转弯的结论题. 首先我们考虑对于某个特定的金币数 \(m\),有哪些 \(n\) 满足条件.考虑最 naive 的情况,\(m= ...
- Codeforces 1491G - Switch and Flip(构造题)
Codeforces 题目传送门 & 洛谷题目传送门 obviously,难度高一点的构造题对我来说都是不可做题 首先考虑将排列拆成一个个置换环,也就是 \(\forall i\) 连边 \( ...
- dlang 泛型
1 import std.stdio, std.string; 2 3 void main() 4 { 5 bool find(T)(T[] all, T sub) 6 { 7 foreach(eac ...
- 一款真正可以拿的出手的本土嵌入式RTOS-SylixOS
由 winniewei 提交于 周四, 12/20/2018 作者:张国斌 在参加工信部人才交流中心和南京浦口区开发区管委会联合举办的第三届集成电路产业紧缺人才创新发展高级研修班暨产业促进交流会期间, ...