[单调栈]Imbalanced Array
I
m
b
a
l
a
n
c
e
d
A
r
r
a
y
Imbalanced Array
ImbalancedArray
题目描述
You are given an array a a a consisting of n n n elements. The imbalance value of some subsegment of this array is the difference between the maximum and minimum element from this segment. The imbalance value of the array is the sum of imbalance values of all subsegments of this array.
For example, the imbalance value of array [1,4,1] [1,4,1] [1,4,1] is 9 9 9 , because there are 6 6 6 different subsegments of this array:
- [1] (from index 1 to index 1 ), imbalance value is 0 ;
- [1,4] (from index 1 to index 2 ), imbalance value is 3 ;
- [1,4,1] (from index 1 to index 3 ), imbalance value is 3 ;
- [4] (from index 2 to index 2 ), imbalance value is 0 ;
- [4,1] (from index 2 to index 3 ), imbalance value is 3 ;
- [1] (from index 3 to index 3 ), imbalance value is 0 ;
You have to determine the imbalance value of the array a .
对于给定由 n 个元素构成的数组。一个子数组的不平衡值是这个区间的最大值与最小值的差值。数组的不平衡值是它所有子数组的不平衡值的总和。
以下是数组[1,4,1]不平衡值为9的例子,共有6个子序列:
[1] (从第一号到第一号)不平衡值为 0;
[1, 4] (从第一号到第二号), 不平衡值为 3;
[1, 4, 1] (从第一号到第三号),不平衡值为 3;
[4] (从第二号到第二号),不平衡值为 0;
[4, 1] (从第二号到第三号),不平衡值为 3;
[1] (从第三号到第三号)不平衡值为 0;
输入
The first line contains one integer n n n ( 1<=n<=106 ) — size of the array a a a .
The second line contains n n n integers a1,a2… an ( 1<=ai<=106) — elements of the array.
输出
Print one integer — the imbalance value of a a a .
样例输入
3
1 4 1
样例输出
9
code
#include<cstdio>
#include<iostream>
using namespace std;
long long n,s,tail,a[1000005],f[1000005],minl[1000005],minr[1000005],maxl[1000005],maxr[1000005];
int main()
{
scanf("%lld",&n);
for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
f[1]=0;
tail=1;
for(int i=1;i<=n;i++)
if(a[i]>a[f[tail]])
{
minl[i]=i-1;
f[++tail]=i;
}
else
{
while(a[i]<a[f[tail]]&&tail>=1)tail--;
minl[i]=f[tail];
f[++tail]=i;
}
f[1]=n+1;
tail=1;
for(int i=n;i>=1;i--)
if(a[i]>a[f[tail]])
{
minr[i]=i+1;
f[++tail]=i;
}
else
{
while(a[i]<=a[f[tail]]&&tail>=1)tail--;
minr[i]=f[tail];
f[++tail]=i;
}
a[0]=a[n+1]=2147483647;
f[1]=0;
tail=1;
for(int i=1;i<=n;i++)
if(a[i]<a[f[tail]])
{
maxl[i]=i-1;
f[++tail]=i;
}
else
{
while(a[i]>a[f[tail]]&&tail>=1)tail--;
maxl[i]=f[tail];
f[++tail]=i;
}
f[1]=n+1;
tail=1;
for(int i=n;i>=1;i--)
if(a[i]<a[f[tail]])
{
maxr[i]=i+1;
f[++tail]=i;
}
else
{
while(a[i]>=a[f[tail]]&&tail>=1)tail--;
maxr[i]=f[tail];
f[++tail]=i;
}
for(int i=1;i<=n;i++)
s+=a[i]*1ll*(1ll*(i-maxl[i])*(maxr[i]-i)-1ll*(i-minl[i])*(minr[i]-i));
printf("%lld",s);
}
[单调栈]Imbalanced Array的更多相关文章
- Educational Codeforces Round 23 D. Imbalanced Array 单调栈
D. Imbalanced Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Imbalanced Array CodeForces - 817D (思维+单调栈)
You are given an array a consisting of n elements. The imbalance value of some subsegment of this ar ...
- codeforces 817 D. Imbalanced Array(单调栈+思维)
题目链接:http://codeforces.com/contest/817/problem/D 题意:给你n个数a[1..n]定义连续子段imbalance值为最大值和最小值的差,要你求这个数组的i ...
- 「10.11」chess(DP,组合数学)·array(单调栈)·ants(莫队,并茶几)
菜鸡wwb因为想不出口胡题所以来写题解了 A. chess 昨天晚上考试,有点困 开考先花五分钟扫了一边题,好开始肝$T1$ 看了一眼$m$的范围很大,第一反应矩阵快速幂?? $n$很小,那么可以打$ ...
- [CF442C] Artem and Array (贪心+单调栈优化)
题目链接:http://codeforces.com/problemset/problem/442/C 题目大意:一个数列,有n个元素.你可以做n-2次操作,每次操作去除一个数字,并且得到这个数字两边 ...
- [CSP-S模拟测试]:array(单调栈)
题目描述 在放完棋子之后,$dirty$又开始了新的游戏. 现在他拥有一个长为$n$的数组$A$,他定义第$i$个位置的分值为$i−k+1$,其中$k$需要满足: 对于任意满足$k\leqslant ...
- 2016 大连网赛---Function(单调栈)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5875 Problem Description The shorter, the simpl ...
- hdu5033 Building (单调栈+)
http://acm.hdu.edu.cn/showproblem.php?pid=5033 2014 ACM/ICPC Asia Regional Beijing Online B 1002 Bui ...
- Max answer(单调栈+ST表)
Max answer https://nanti.jisuanke.com/t/38228 Alice has a magic array. She suggests that the value o ...
随机推荐
- Baccarat中挖矿、兑换和做市的三角关系是什么?
NGK在这波DeFi潮中,推出了Baccarat,为用户带来了流动性挖矿收益,今天笔者就讲一讲Baccarat中挖矿.兑换和做市的关系. 兑换和做市是什么关系呢?众所周知,换币者,是用一种货币去换另一 ...
- 类属性和__init__的实例属性有何区别?进来了解一下吧
真的是随笔写的一篇,以防日后记忆模糊,特此记录.大佬勿喷 疑问:类属性和实例属性有何区别? 正题,代码如下 age为People类的属性(称为类属性) name是在__init__方法下,在创建实例对 ...
- 数据库范式(1NF/2NF/3NF)
本文转载自数据库范式(1NF/2NF/3NF) 概述 范式:英文名称是 Normal Form,它是英国人 E.F.Codd(关系数据库的老祖宗)在上个世纪70年代提出关系数据库模型后总结出来的,范式 ...
- alpakka-kafka(2)-consumer
alpakka-kafka-consumer的功能描述很简单:向kafka订阅某些topic然后把读到的消息传给akka-streams做业务处理.在kafka-consumer的实现细节上,为了达到 ...
- banner自用图床
放些常用的图做图床,也不在别的平台用.
- Python序列之列表(一)
在Python中,列表是一种常用的序列,接下来我来讲一下关于Python中列表的知识. 列表的创建 Python中有多种创建列表的方式 1.使用赋值运算符直接赋值创建列表 在创建列表时,我们直接使用赋 ...
- 微信小程序:Navigator导航组件
导航组件:类似超链接标签. url:要跳转的页面路径,可以放绝对路径,也可以放相对路径,绝对路径指从pages作为根目录开始找到你要的页面. 找到你要找的页面的相对地址的方法:在vscode中,该页面 ...
- 分布式流转开发常见报错FAQ
鸿蒙入门指南,小白速来!0基础学习路线分享,高效学习方法,重点答疑解惑--->[课程入口] HarmonyOS开发中分布式协同是非常重要的一个功能,大家在刚接触的时候可能会出现各种各样的错误.我 ...
- python2与python3共存时的pip问题
在树莓派上同时安装有python2和python3,初始的pip是9.01版本,用pip install django只能安装到1.11版本,但是我需要2.0的django. 于是升级pip: pyt ...
- MySQL注入与informantion_schema库
目录 只可读 自动开启 和MySQL注入有关的3个表 手动注入的使用案例 表介绍 查询一个表中全部字段的过程 MySQL V5.0安装完成会默认会生成一个库(informantion_schema), ...