D. Imbalanced Array

题目链接

D. Imbalanced Array

求所有连续子区间的最大值之和减所有连续子区间最小值之和

Input

The first line contains one integer n(1n106) — size of the array a.

The second line contains n integers a1,a2...an(1ai106) — elements of the array.

Output

Print one integer — the imbalance value of a.

解题思路

单调栈

可以计算每个值作为最小值和最大值时的贡献,以最小值为例:即找左右两边第一个比其大的数,可利用单调栈实现,但由于会有重复计算,即一段区间内出现多个相同的值,这时可选择寻找第一个左边大于或等于和右边大于的数

  • 时间复杂度:O(n)

代码

// %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } template <typename T> inline void print(T x) { if(x<0) { putchar('-'); x=-x; } if(x>9) print(x/10); putchar(x%10+'0'); } const int N=5e5+5; stack<LL> stk; LL sl[N],sr[N],bl[N],br[N],n,a[N]; map<pair<int,PLL>,bool> mp1,mp2; int main() { read(n); for(int i=1;i<=n;i++)read(a[i]); for(int i=1;i<=n;i++) { while(stk.size()&&a[stk.top()]>a[i])stk.pop(); if(stk.size())sl[i]=stk.top()+1; else sl[i]=1; stk.push(i); } while(stk.size())stk.pop(); for(int i=1;i<=n;i++) { while(stk.size()&&a[stk.top()]<a[i])stk.pop(); if(stk.size())bl[i]=stk.top()+1; else bl[i]=1; stk.push(i); } while(stk.size())stk.pop(); for(int i=n;i;i--) { while(stk.size()&&a[stk.top()]>=a[i])stk.pop(); if(stk.size())sr[i]=stk.top()-1; else sr[i]=n; stk.push(i); } while(stk.size())stk.pop(); for(int i=n;i;i--) { while(stk.size()&&a[stk.top()]<=a[i])stk.pop(); if(stk.size())br[i]=stk.top()-1; else br[i]=n; stk.push(i); } LL res=0; for(int i=1;i<=n;i++) res+=a[i]*((i-bl[i]+1)*(br[i]-i+1)-(i-sl[i]+1)*(sr[i]-i+1)); print(res); return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/15942162.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(71)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示