单调栈求调!
查看原帖
单调栈求调!
519573
Daniel_yao楼主2022/4/11 23:25
#include<bits/stdc++.h>
using namespace std;
const int N = 10005;

struct Node{
  int id, v;
};
int n, h[N], tot, sum;
Node stk[N];
int main(){
  cin >> n;
  for(int i = 1;i <= n;i++){
    cin >> h[i];
    while(tot && stk[tot].v <= h[i]){
      sum += (abs(stk[tot].v - h[i])) * (abs(stk[tot].id - i));
      tot--;
    }
    stk[++tot] = {i, h[i]};
  }
  cout << sum << endl;
  return 0;
}

2022/4/11 23:25
加载中...