LINK

题意:给出n个数,求所有子区间的最大最小值差的和。

思路:过去有道题目是求所有子区间的最大值或最小值,这题类似,我们对每一个数计算其作为最大值得次数和最小值的次数,这两个值求法类似,都是比左侧数大(小)的数量*比右侧数大(小)数量。

/** @Date    : 2017-07-02 15:24:36
* @FileName: 817D 线性求子区间最大最小 思维.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e6+20;
const double eps = 1e-8; int n, a[N];
LL lm[N], rm[N];
LL li[N], ri[N];
int main()
{
while(cin >> n)
{
for(int i = 1; i <= n; i++) scanf("%d", a + i); for(int i = 1; i <= n; i++) lm[i] = rm[i] = li[i] = ri[i] = i; for(int i = 2; i <= n; i++)
{
int p = i;
while(a[i] >= a[p - 1] && p - 1 > 0)//注意大于小于的边界
p = lm[p - 1];
lm[i] = p; p = i;
while(a[i] < a[p - 1] && p - 1 > 0)
p = li[p - 1];
li[i] = p;
}
for(int i = n - 1; i >= 1; i--)
{
int p = i;
while(a[i] > a[p + 1] && p + 1 <= n)//不能重合
p = rm[p + 1];
rm[i] = p; p = i;
while(a[i] <= a[p + 1] && p + 1 <= n)
p = ri[p + 1];
ri[i] = p;
} LL ans = 0;
for(int i = 1; i <= n; i++)
{
//cout << li[i] << "~" << ri[i] << endl;
ans += a[i] * ((LL)(rm[i] - i + 1)*(i - lm[i] + 1) - (LL)(ri[i] - i + 1)*(i - li[i] + 1));
}
printf("%lld\n", ans);
}
return 0;
}
//这题和以往的维护子区间的最大最小值的思维一样
//要线性复杂度下求最大值和最小值显然是预处理数列,对每个数求其左右侧 大于/小于的个数
//这题要求为最大值-最小值所有子区间和
//那么意味着得到每个数分别作为最大值和最小值的次数就很轻松能求得

817D. Imbalanced Array 预处理最大最小 思维的更多相关文章

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

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

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

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

  3. 从包含10个无符号数的字节数组array中选出最小的一个数存于变量MIN中,并将该数以十进制形式显示出来。

    问题 从包含10个无符号数的字节数组array中选出最小的一个数存于变量MIN中,并将该数以十进制形式显示出来. 代码 data segment arrey db 0,1,2,4,6,5,7,9,8, ...

  4. [单调栈]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 con ...

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

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

  6. Beauty of Array ZOJ - 3872(思维题)

    Edward has an array A with N integers. He defines the beauty of an array as the summation of all dis ...

  7. 51nod 1096 距离之和最小 思维题,求中位数

    题目: 在一条直线上,与两个点距离之和最小的点,是怎样的点? 很容易想到,所求的点在这两个已知点的中间,因为两点之间距离最短. 在一条直线上,与三个点距离之和最小的点,是怎样的点? 由两个点的规律,我 ...

  8. D. Imbalanced Array

    让你计算所有连续子序列的最大值-最小值的和. (单调栈) 对于一个数Ai来讲,如果其有贡献的价值,要么是-Ai作为最小值,要么是Ai作为最大值. 那么Ans=ΣAi*maxn-Ai*minn. voi ...

  9. codeforces 361 C. Levko and Array Recovery(暴力+思维)

    题目链接:http://codeforces.com/contest/361/problem/C 题意:对一个数列有这么两个操作 1.(1,l,r,p)..将区间[l,r]所有数都加上p 2.(2,l ...

随机推荐

  1. TDGA-需求分析

    李青:绝对的技术控,团队中扮演“猪”的角色,勤干肯干,是整个团队的主心骨,课上紧跟老师的步伐,下课谨遵老师的指令,课堂效率高,他的编程格言“没有编不出来的程序,只有解决不了的bug”. 胡金辉:半两油 ...

  2. txt文件存储问题

    一.实际大小与占用空间不一致: 1.占用空间和磁盘有关,一般磁盘存储最小大小为4kb(4096字节). 2.当txt文件中仅有1个数字‘5’的时候,大小显示为1个字节(属性看,列表详细不精确),占用空 ...

  3. 使用git下载编译erlang

    git clone https://github.com/erlang/otp cd otp git tag git checkout -b OTP- OTP- ./otp_build all exp ...

  4. Spring配置声明

    <...     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xmlns:p="htt ...

  5. libnl3.2.25安装编译

    1,tar zxvf libnl-3.2.25.tar.gz 2,cd libnl-3.2.25 3,./configure --prefix=/usr  --sysconfdir=/etc  --d ...

  6. 秒杀多线程第二篇 多线程第一次亲密接触 CreateThread与_beginthreadex本质区别(续)

    由于原作者主要写window上的线程,而我主要学习android,所以本文将分析android方面多线程. 1.Thread: public void Thread1(){ Thread a = ne ...

  7. 插件-监控页面加载之loading

    查看效果点https://icedjuice.github.io/plug-in/loading/loading.html 简单易用的loading插件,该插件并不是真正的监控页面的资源加载过程,而是 ...

  8. 扔几道sb题

    1.给定一个长度为N的数列,A1, A2, ... AN,如果其中一段连续的子序列Ai, Ai+1, ... Aj(i <= j)之和是K的倍数,我们就称这个区间[i, j]是K倍区间. 你能求 ...

  9. arp 投毒实验

    1.查看kali2.0和kali2.0.0的IP地址,如图1和图2,其中192.168.1.133作为攻击者,192.168.1.109作为PC访问FTP服务器192.168.1.234 图1 图2 ...

  10. 通过系统自带的MSI安装包来提权账号

    Windows environments provide a group policy setting which allows a regular user to install a Microso ...