Processing math: 100%

AT2060 [AGC005B] Minimum Sum

题面

给你一个长为 n 的数列 a,求

nl=1nr=lmin{alar}

思路

考试题改的。

可以用单调栈算出贡献区间,然后乘法原理计算出总贡献。

具体见代码:

代码

// O(n)
#include <bits/stdc++.h>
#define int long long
using namespace std;

int a[300005];
int l[300005], r[300005];
int ll[300005], rr[300005];
int n;
int top, top2;
int st[300005];
int st2[300005];
long long ans;
//int ans1,ans2;
signed main() {
	//freopen("sequence.in", "r", stdin);
	//freopen("sequence.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		r[i] = n + 1;
		rr[i] = n + 1;
	}
	for (int i = 1; i <= n; ++i) {
		while (top && a[st[top]] > a[i]) {
			r[st[top--]] = i;
		}
		l[i] = st[top];
		st[++top] = i;
	}
	for (int i = 1; i <= n; i++) {
		ans += (r[i] - i) * (i - l[i]) * a[i];
		//ans += (rr[i] - i) * (i - ll[i]) * a[i];
	}
	cout << ans << '\n';
	return 0;
}

posted @   蒟蒻xiezheyuan  阅读(21)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 因为Apifox不支持离线,我果断选择了Apipost!
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示