817D. Imbalanced Array 预处理最大最小 思维
题意:给出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 预处理最大最小 思维的更多相关文章
- codeforces 817 D. Imbalanced Array(单调栈+思维)
题目链接:http://codeforces.com/contest/817/problem/D 题意:给你n个数a[1..n]定义连续子段imbalance值为最大值和最小值的差,要你求这个数组的i ...
- Educational Codeforces Round 23 D. Imbalanced Array 单调栈
D. Imbalanced Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 从包含10个无符号数的字节数组array中选出最小的一个数存于变量MIN中,并将该数以十进制形式显示出来。
问题 从包含10个无符号数的字节数组array中选出最小的一个数存于变量MIN中,并将该数以十进制形式显示出来. 代码 data segment arrey db 0,1,2,4,6,5,7,9,8, ...
- [单调栈]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 ...
- Imbalanced Array CodeForces - 817D (思维+单调栈)
You are given an array a consisting of n elements. The imbalance value of some subsegment of this ar ...
- 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 ...
- 51nod 1096 距离之和最小 思维题,求中位数
题目: 在一条直线上,与两个点距离之和最小的点,是怎样的点? 很容易想到,所求的点在这两个已知点的中间,因为两点之间距离最短. 在一条直线上,与三个点距离之和最小的点,是怎样的点? 由两个点的规律,我 ...
- D. Imbalanced Array
让你计算所有连续子序列的最大值-最小值的和. (单调栈) 对于一个数Ai来讲,如果其有贡献的价值,要么是-Ai作为最小值,要么是Ai作为最大值. 那么Ans=ΣAi*maxn-Ai*minn. voi ...
- 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 ...
随机推荐
- idea的快捷键(复制)
IntelliJ Idea 常用快捷键列表 Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Sh ...
- Codeforces Round #335 (Div. 2) D. Lazy Student 贪心+构造
题目链接: http://codeforces.com/contest/606/problem/D D. Lazy Student time limit per test2 secondsmemory ...
- Codeforces Beta Round #8 C. Looking for Order 状压dp
题目链接: http://codeforces.com/problemset/problem/8/C C. Looking for Order time limit per test:4 second ...
- caffe神经网络模型的绘图
Python/draw_net.py, 这个文件,就是用来绘制网络模型的.也就是将网络模型由prototxt变成一张图片. 1.安装GraphViz # sudo apt-get install Gr ...
- laravel5.6 调用第三方类库
大概流程: 1. 新建一个目录方类库 2. 配置composer配置文件 3. 在项目中使用终端运行composer dumpautoload 4. 使用时 方法调用可以new对象后->方法名 ...
- ViewPager、Fragment、Matrix综合使用实现Tab滑页效果
原文地址:http://www.cnblogs.com/kross/p/3372987.html 我们实现一个上面是一个可以左右滑动的页面,下面是三个可点击切换的tab按钮,tab按钮上还有一个激活条 ...
- 【Linux笔记】Linux中inittab剖析
Linux完成内核(Kernel)引导后,会由init初始化进程调用/etc/inittab配置文件(ps -aux | less,init进程号为始终为1,是所有系统进程的起点,init进程也有一个 ...
- bug:margin合并
demo1和demo2存在margin合并问题:外边距合并指的是,当两个垂直外边距相遇时,它们将形成一个外边距.合并后的外边距的高度等于两个发生合并的外边距的高度中的较大者.弥补方案:bfc; 添加一 ...
- 表单验证2-JS正则
1. JS正则: 以/开头,以/结尾. test作用:到里面去找,只要里面有,就返回true:否则就返回false. 例如:rep=/\d+/; 检验里面是否有数字. 2.rep=/^ $/; ...
- Zookeeper(二) zookeeper集群搭建 与使用
一.zookeeper集群搭建 鉴于 zookeeper 本身的特点,服务器集群的节点数推荐设置为奇数台.我这里我规划为三台, 为别为 hadoop01,hadoop02,hadoop03 1. ...