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

  1. #include<cstdio>
  2. #include<iostream>
  3. using namespace std;
  4. long long n,s,tail,a[1000005],f[1000005],minl[1000005],minr[1000005],maxl[1000005],maxr[1000005];
  5. int main()
  6. {
  7. scanf("%lld",&n);
  8. for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
  9. f[1]=0;
  10. tail=1;
  11. for(int i=1;i<=n;i++)
  12. if(a[i]>a[f[tail]])
  13. {
  14. minl[i]=i-1;
  15. f[++tail]=i;
  16. }
  17. else
  18. {
  19. while(a[i]<a[f[tail]]&&tail>=1)tail--;
  20. minl[i]=f[tail];
  21. f[++tail]=i;
  22. }
  23. f[1]=n+1;
  24. tail=1;
  25. for(int i=n;i>=1;i--)
  26. if(a[i]>a[f[tail]])
  27. {
  28. minr[i]=i+1;
  29. f[++tail]=i;
  30. }
  31. else
  32. {
  33. while(a[i]<=a[f[tail]]&&tail>=1)tail--;
  34. minr[i]=f[tail];
  35. f[++tail]=i;
  36. }
  37. a[0]=a[n+1]=2147483647;
  38. f[1]=0;
  39. tail=1;
  40. for(int i=1;i<=n;i++)
  41. if(a[i]<a[f[tail]])
  42. {
  43. maxl[i]=i-1;
  44. f[++tail]=i;
  45. }
  46. else
  47. {
  48. while(a[i]>a[f[tail]]&&tail>=1)tail--;
  49. maxl[i]=f[tail];
  50. f[++tail]=i;
  51. }
  52. f[1]=n+1;
  53. tail=1;
  54. for(int i=n;i>=1;i--)
  55. if(a[i]<a[f[tail]])
  56. {
  57. maxr[i]=i+1;
  58. f[++tail]=i;
  59. }
  60. else
  61. {
  62. while(a[i]>=a[f[tail]]&&tail>=1)tail--;
  63. maxr[i]=f[tail];
  64. f[++tail]=i;
  65. }
  66. for(int i=1;i<=n;i++)
  67. s+=a[i]*1ll*(1ll*(i-maxl[i])*(maxr[i]-i)-1ll*(i-minl[i])*(minr[i]-i));
  68. printf("%lld",s);
  69. }

[单调栈]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. macOS open url from terminal

    macOS open url from terminal open URL && start terminal bash open url in chrome open chrome ...

  2. Flutter: random color

    import 'dart:math' as math; import 'package:flutter/material.dart'; void main() => runApp(App()); ...

  3. JUnit5学习之二:Assumptions类

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  4. vue:表格中多选框的处理

    效果如下: template中代码如下: <el-table v-loading="listLoading" :data="list" element-l ...

  5. Tango with django 1.9 中文——3.Django基础

    让我们开始运用Django.本章主要是给你一个关于创建新项目和新应用过程的概览.在本章的末尾,你将建立起一个简单的由Django驱动的网站. 3.1 配置测试 让我们测试以下你的Python和Djan ...

  6. 网络地址转换NAT的两种模式(概念浅析)& IP溯源

    由于全球IPv4地址越来越少.越来越贵,因此大到一个组织,小到一个家庭一个人都很难获得公网IP地址,所以只能使用内网地址,从而和别人共享一个公网IP地址.在这种情况下,NAT技术诞生. 翻译 NAT( ...

  7. HDOJ-4027(线段树+区间更新(每个节点更新的值不同))

    Can You answer these queries? HDOJ-4027 这道题目和前面做的题目略有不同.以前的题目区间更新的时候都是统一更新的,也就是更新相同的值.但是这里不一样,这里更新的每 ...

  8. Java-Socket通信 知识点记录

    目录 一.Socket基本案例 二.消息通信 2.1 双向通信 2.2 告知发送结束 2.2.1 通过Socket关闭 2.2.2 通过Socket关闭输出流的方式 2.2.3 通过约定符号 2.2. ...

  9. Python学习笔记 CH1-4:从入门到列表

    Python CH1 环境准备 因为已经有了C/C++.Java的基础,所以上手很快. 参考书:Eric Matthes -<Python编程 从入门到实践> 环境准备:python3.P ...

  10. Centos mini系统下的Hadoop集群搭建

    1.事前了解 1.1 Hadoop 百度百科:https://baike.baidu.com/item/Hadoop/3526507?fr=aladdin Hadoop是一个由Apache基金会所开发 ...