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的更多相关文章

  1. Educational Codeforces Round 23 D. Imbalanced Array 单调栈

    D. Imbalanced Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. Imbalanced Array CodeForces - 817D (思维+单调栈)

    You are given an array a consisting of n elements. The imbalance value of some subsegment of this ar ...

  3. codeforces 817 D. Imbalanced Array(单调栈+思维)

    题目链接:http://codeforces.com/contest/817/problem/D 题意:给你n个数a[1..n]定义连续子段imbalance值为最大值和最小值的差,要你求这个数组的i ...

  4. 「10.11」chess(DP,组合数学)·array(单调栈)·ants(莫队,并茶几)

    菜鸡wwb因为想不出口胡题所以来写题解了 A. chess 昨天晚上考试,有点困 开考先花五分钟扫了一边题,好开始肝$T1$ 看了一眼$m$的范围很大,第一反应矩阵快速幂?? $n$很小,那么可以打$ ...

  5. [CF442C] Artem and Array (贪心+单调栈优化)

    题目链接:http://codeforces.com/problemset/problem/442/C 题目大意:一个数列,有n个元素.你可以做n-2次操作,每次操作去除一个数字,并且得到这个数字两边 ...

  6. [CSP-S模拟测试]:array(单调栈)

    题目描述 在放完棋子之后,$dirty$又开始了新的游戏. 现在他拥有一个长为$n$的数组$A$,他定义第$i$个位置的分值为$i−k+1$,其中$k$需要满足: 对于任意满足$k\leqslant ...

  7. 2016 大连网赛---Function(单调栈)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5875 Problem Description The shorter, the simpl ...

  8. hdu5033 Building (单调栈+)

    http://acm.hdu.edu.cn/showproblem.php?pid=5033 2014 ACM/ICPC Asia Regional Beijing Online B 1002 Bui ...

  9. Max answer(单调栈+ST表)

    Max answer https://nanti.jisuanke.com/t/38228 Alice has a magic array. She suggests that the value o ...

随机推荐

  1. WebAssembly JavaScript APIs

    WebAssembly JavaScript APIs https://developer.mozilla.org/en-US/docs/WebAssembly/Using_the_JavaScrip ...

  2. nodejs 查看进程表

    psaux tasklist system-tasks const { exec } = require("child_process"); const isWindows = p ...

  3. 「NGK每日快讯」2021.2.4日NGK公链第93期官方快讯!

  4. NGK.IO的智能合约是炒作还是未来商业的主流?

    随着NGK主网的上线,NGK市场也备受关注.目前,NGK代币价格已经由初始价格0.0215美元涨到现在的0.86美元,代币价格上涨40倍!数字货币市场也已经将重点目光放到了NGK代币上,相信在不久的将 ...

  5. C语言数据类型思维导图

  6. 前端axios传递一个包含数组的对象到后台,后台可以用String接收,也可以用List集合接收

    前端代码: data() { return { listQuery: { date: [], } }}, //查询列表信息getList() { if (this.listQuery.date == ...

  7. 关于电脑硬盘的二三事(SATA接口)

    @ 目录 前言 接口分类 SATA3接口 机械硬盘 机械硬盘的特点和主要参数 西部数据机械盘分类 绿·蓝·黑盘 红盘 紫盘 金盘 希捷机械盘分类 酷狼 酷鱼 酷鹰 银河 SATA3接口的固态硬盘 固态 ...

  8. Java基本概念:面向对象

    一.简介 面向过程的思维模式是简单的线性思维,思考问题首先陷入第一步做什么.第二步做什么的细节中. 面向对象的思维模式说白了就是分类思维模式.思考问题首先会解决问题需要哪些分类,然后对这些分类进行单独 ...

  9. Linux安装jdk(两种方式)

    最近在研究大数据方面的东西,业务场景是从设备采集数据经过处理然后存放DB. 建设上面的环境第一步肯定是安装jdk,所以和大家一起学一下基本知识centos7.5安装jdk1.8. 安装jdk有两种方法 ...

  10. GDB调试:从入门到入土

    GDB是类Unix操作糸统下使用命令行调试的调试软件,全名GNU Debugger,在NOI系列竞赛使用的NOI Linux系统中起很大作用(如果不想用毒瘤Guide或直接输出)(XXX为文件名) 1 ...