Imbalanced Array CodeForces - 817D (思维+单调栈)
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 valuesof all subsegments of this array.
For example, the imbalance value of array [1, 4, 1] is 9, because there are 6different 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.
Input
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.
Output
Print one integer — the imbalance value of a.
Example
3
1 4 1
9 题意:
给定一个含有N个数的数组,求这个数组的所有连续区间的不平衡值的总和。
一个区间的不平衡值为这个区间中的最大值减去最小值。
思路:
我们可以这样思考,这个题目的答案可以这样得来,
对于每一个a[i],它对答案的贡献值为以a[i]为区间最大值的区间数量*a[i]-a[i]*以a[i]为区间最小值的区间数量。
ans=sum{ contribution(a[i])} 那么我们的难题为如何计算以a[i]为区间最大/最小值的区间数量。
这种模型我们很容易想到利用单调栈来O(N)求出。
我们定义两个数组,l[n+5],r[n+5],l[i]和r[i] 分别维护的是从a[i]元素开始向左和向右第一个比a[i]小的元素的位置。(注意边界)
然后我们可以通过l[i]和r[i]来求出区间数量,然后我们再利用单调栈求出从a[i]元素开始向左和向右第一个比a[i]大的元素的位置
然后我们愉快的计算每一个的贡献然后加起来就是我们要求的答案。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rt return
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
int a[maxn];
int l[maxn];
int r[maxn];
ll ans=0ll;
int main()
{
gg(n);
repd(i,,n)
{
gg(a[i]);
}
stack<int> s;
repd(i,,n)
{
while(s.size()&&a[s.top()]>a[i])
{
s.pop();
}
if(s.size())
{
l[i]=s.top();
}else
{
l[i]=;
}
s.push(i);
}
while(s.size())
{
s.pop();
}
for(int i=n;i>=;i--)
{
while(s.size()&&a[s.top()]>=a[i])
{
s.pop();
}
if(s.size())
{
r[i]=s.top();
}else
{
r[i]=n+;
}
s.push(i);
}
repd(i,,n)
{
ans=ans-1ll*a[i]*(i-l[i])*(r[i]-i);
}
// db(ans);
while(s.size())
{
s.pop();
}
repd(i,,n)
{
while(s.size()&&a[s.top()]<a[i])
{
s.pop();
}
if(s.size())
{
l[i]=s.top();
}else
{
l[i]=;
}
s.push(i);
}
while(s.size())
{
s.pop();
}
for(int i=n;i>=;i--)
{
while(s.size()&&a[s.top()]<=a[i])
{
s.pop();
}
if(s.size())
{
r[i]=s.top();
}else
{
r[i]=n+;
}
s.push(i);
} repd(i,,n)
{
ans=ans+1ll*a[i]*(i-l[i])*(r[i]-i);
}
printf("%lld\n",ans );
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Imbalanced Array CodeForces - 817D (思维+单调栈)的更多相关文章
- Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)
https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...
- Psychos in a Line CodeForces - 319B (单调栈的应用)
Psychos in a Line CodeForces - 319B There are n psychos standing in a line. Each psycho is assigned ...
- Largest Submatrix of All 1’s(思维+单调栈)
Given a m-by-n (0,1)-matrix, of all its submatrices of all 1's which is the largest? By largest we m ...
- Mike and Feet CodeForces - 548D (单调栈)
Mike is the president of country What-The-Fatherland. There are n bears living in this country besid ...
- Maximum Xor Secondary CodeForces - 281D (单调栈)
Bike loves looking for the second maximum element in the sequence. The second maximum element in the ...
- Stack Sorting CodeForces - 911E (思维+单调栈思想)
Let's suppose you have an array a, a stack s (initially empty) and an array b (also initially empty) ...
- codeforces 547B【单调栈】
题意: 有一个长度为n的序列,序列有长度为1...n的连续子序列, 一个连续子序列里面最小的值称作这个子序列的子序列的strength, 要求出每种长度的连续子序列的最大的strength. 思路: ...
- 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 ...
随机推荐
- python轻量级数据存储
python为开发者提供了一个轻量级的数据存储方式shelve,对于一些轻量数据,使用shelve是个比较不错的方式.对于shelve,可以看成是一个字典,它将数据以文件的形式存在本地.下面介绍具体用 ...
- Linux端口映射,80端口映射到8080端口
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080 其中eth0为外网网卡名称 ipt ...
- C语言 设一个函数process,调用它时,实现不同功能。
//凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 输入a, b,第一次调用process找最大值,第二次调用process找最小值,第三次调用求和. 方法1 ...
- Maven将中央仓库修改为阿里云的仓库地址
<mirror> <id>nexus-aliyun</id> <mirrorOf>*</mirrorOf> <name>Nexu ...
- Vue修改、编辑时,撤销修改内容,表格内容不变
在编辑该行的过程中,突然不想编辑了,想点击撤销按钮,将该行数据恢复到旧值,目前的做法是,在点击编辑按钮的时候转换成json字符,点击撤销按钮的时候再解析成对象,赋值给该行的数据. // 编辑editH ...
- 关于陌生的依赖模块,如withStyles、react-apollo等
有自己不认识的依赖,可参考的学习方式: 1.各大技术分享网站的文章(最快) 2.npm官网下的文档(最全)
- 8.oop-多态
一.继承1.定义:子类继承父类,会继承父类的属性和方法2.语法:extends关键字 子类 extends 父类3.特点:java中的继承是单一继承,子类只能继承一个父类,但是父类可以有多个子类4.用 ...
- 【转】打包 压缩 命令tar zip
https://www.cnblogs.com/centos2017/p/7896807.html tar语法 #压缩tar -czvf ***.tar.gztar -cjvf ***.tar.bz2 ...
- C++ —— 返回数组指针的函数 和 返回指向函数的指针的函数
返回数组指针的函数 基础知识:数组不能被拷贝,函数不能返回数组,只能返回数组的指针或者引用. 定义一个 返回数组指针的函数 的方法,以 一个接收参数为 含有10个整型元素的数组的引用 和 返回一个含 ...
- c# 利用百度图像处理【人像分割】一键抠图
百度AI开放平台-人像分割: http://ai.baidu.com/tech/body/seg 注意本文后面的话,百度这个技术效果太差劲了,国外这 https://www.remove.bg/ 个比 ...