zoukankan      html  css  js  c++  java
  • [Agc005D/At2060] Minimum Sum

    鉴于早上那题让我怀疑单调栈白学,特意来复习下单调栈

    题意

    考虑按照每个元素对答案的贡献来统计,那么我们只需要找到每个元素左边右边第一个比它小的就可

    这题给的又是排列,简直不能再良心

    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    const int N = 1000005;
    
    int s[N],top,a[N],l[N],r[N],n,ans;
    
    signed main() {
        cin>>n;
        for(int i=1;i<=n;i++) {
            cin>>a[i];
        }
        for(int i=1;i<=n;i++) {
            while(top && a[i]<a[s[top]]) r[s[top]]=i, --top;
            s[++top]=i;
        }
        while(top) r[s[top]]=n+1, --top;
        for(int i=n;i>=1;--i) {
            while(top && a[i]<a[s[top]]) l[s[top]]=i, --top;
            s[++top]=i;
        }
        while(top) l[s[top]]=0, --top;
        for(int i=1;i<=n;i++) {
            ans += a[i] * (r[i]-i) * (i-l[i]);
        }
        cout<<ans;
    }
    
  • 相关阅读:
    CCCC练习即感
    1003 我能通过
    录制开讲啦杂感
    OOP第三次上机
    关于C++随机函数
    蓝桥杯杂感。
    CF502C The Phone Number
    It's a secret
    2017-06-22
    2017-05-12
  • 原文地址:https://www.cnblogs.com/mollnn/p/12271491.html
Copyright © 2011-2022 走看看