Educational Codeforces Round 23 D. Imbalanced Array 单调栈
2 seconds
256 megabytes
standard input
standard output
You are given an array a consisting of 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] is 9, because there are 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.
The first line contains one integer n (1 ≤ n ≤ 106) — size of the array a.
The second line contains n integers a1, a2... an (1 ≤ ai ≤ 106) — elements of the array.
Print one integer — the imbalance value of a.
3
1 4 1
9
题意:给你n个数,一个区间的贡献是区间最大值-区间最小值,求所有区间贡献和。
思路:首先想到n*n的rmq,明显超时。
换个角度,枚举每个数作为最小值的区间个数,枚举每个数作为最大值的区间个数;
怎么算呢,例如求最小值的区间,找出左边有x个比其大的,右边x个比其大的,区间的个数为x*y;
这个用单调栈可以O(n)处理,注意需要半开半闭;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<bitset>
#include<time.h>
using namespace std;
#define LL long long
#define pi (4*atan(1.0))
#define eps 1e-4
#define bug(x) cout<<"bug"<<x<<endl;
const int N=1e6+,M=1e6+,inf=,mod=1e9+;
const LL INF=1e18+,MOD=1e9+; int a[N],d[N];
int zn[N],yn[N];
int zx[N],yx[N];
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
a[]=;
int k=;d[++k]=;
for(int i=;i<=n;i++)
{
while(a[d[k]]>=a[i])k--;
zn[i]=d[k];
d[++k]=i;
}
a[n+]=;
k=;d[++k]=n+;
for(int i=n;i>=;i--)
{
while(a[d[k]]>a[i])k--;
yn[i]=d[k];
d[++k]=i;
}
a[]=1e6+;
k=;d[++k]=;
for(int i=;i<=n;i++)
{
while(a[d[k]]<=a[i])k--;
zx[i]=d[k];
d[++k]=i;
}
a[n+]=1e6+;
k=;d[++k]=n+;
for(int i=n;i>=;i--)
{
while(a[d[k]]<a[i])k--;
yx[i]=d[k];
d[++k]=i;
}
LL ans=;
for(int i=;i<=n;i++)
{
ans+=1LL*(yx[i]-i)*(i-zx[i])*a[i];
ans-=1LL*(yn[i]-i)*(i-zn[i])*a[i];
}
cout<<ans<<endl;
return ;
}
Educational Codeforces Round 23 D. Imbalanced Array 单调栈的更多相关文章
- [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)
Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...
- Educational Codeforces Round 23 A-F 补题
A Treasure Hunt 注意负数和0的特殊处理.. 水题.. 然而又被Hack了 吗的智障 #include<bits/stdc++.h> using namespace std; ...
- Educational Codeforces Round 23 补题小结
昨晚听说有教做人场,去补了下玩. 大概我的水平能做个5/6的样子? (不会二进制Trie啊,我真菜) A. 傻逼题.大概可以看成向量加法,判断下就好了. #include<iostream> ...
- Educational Codeforces Round 11 A. Co-prime Array 水题
A. Co-prime Array 题目连接: http://www.codeforces.com/contest/660/problem/A Description You are given an ...
- Educational Codeforces Round 23 B. Makes And The Product
B. Makes And The Product time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Educational Codeforces Round 23 E. Choosing The Commander trie数
E. Choosing The Commander time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- Educational Codeforces Round 63 D. Beautiful Array
D. Beautiful Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Educational Codeforces Round 11——A. Co-prime Array(map+vector)
A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Educational Codeforces Round 23.C
C. Really Big Numbers time limit per test 1 second memory limit per test 256 megabytes input standar ...
随机推荐
- RHEL7 CentOS7 的 firewall命令简单介绍
firewall 服务介绍 firewall 服务是 redhat7 和 centos7 系统默认安装好的防火墙服务,一个信任级别的概念来管理与之相关联的连接与接口.它支持 ipv4 与 ipv6,并 ...
- [转载]window.location.href的用法(动态输出跳转)
无论在静态页面还是动态输出页面中window.location.href都是不错的用了跳转的实现方案 javascript中的location.href有很多种用法,主要如下. self.loca ...
- <转>jmeter(六)元件的作用域与执行顺序
本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...
- Logstash 安装和使用
下载地址 https://artifacts.elastic.co/downloads/logstash/logstash-5.6.8.zip 下载后解压,测试 #将键盘内容输出到控制台 logsta ...
- EDK II之DXE Core框架简介
本文旨在简单的介绍一下DXE阶段的工作原理: UDK2015的开源代码下载:https://github.com/tianocore/tianocore.github.io/wiki/EDK-II D ...
- lnmp重置密码
wget http://soft.vpser.NET/lnmp/ext/reset_mysql_root_password.sh;sh reset_mysql_root_password.sh
- 判断PC或mobile设备
js 限制: <script type="text/javascript"> function uaredirect(f){try{if(document.getEle ...
- Mysql去掉html标签函数
函数 SET GLOBAL log_bin_trust_function_creators=; DROP FUNCTION IF EXISTS fnStripTags; DELIMITER | CRE ...
- Zookeeper本地模式安装
本地模式安装部署 1.安装前准备 (1)安装Jdk (2)拷贝Zookeeper安装包到Linux系统下 (3)解压到指定目录 tar -zxvf zookeeper-3.4.10.tar.gz -C ...
- 标准库 svc—程序及服务控制
对于程序及服务的控制,本质上而言就是正确的启动,并可控的停止或退出.在go语言中,其实就是程序安全退出.服务控制两个方面.核心在于系统信号获取.Go Concurrency Patterns.以及基本 ...