• [nowcoderACM_223C][区区区间间间]


    题目链接

    思路

    考虑用单调栈,栈顶为最大元素。当得到一个新值得时候,将这个值宇栈顶比较。因为栈顶是前面的最大元素。所以只要当前元素比栈顶大,那么肯定比前面的都大。只要将这个元素乘上前面的个数就行了。

    然后考虑对于像5,4这样的情况应该怎么办。因为4比5小,所以当前四的贡献就是将前面的每个值都加上一遍。

    代码

    #include<cstdio>
    #include<iostream>
    using namespace std;
    typedef long long ll;
    const int N = 100000 +100;
    ll a[N],q[N];
    ll n;
    ll work() {
        int tail = 0;
        ll now = 0,ans = 0;
        for(int i = 1; i <= n;++i) {
            while(a[i] > a[q[tail]] && tail) now -= a[q[tail]] * (q[tail] - q[tail - 1]),tail--;
            q[++tail] = i;
            now += a[i] * (q[tail] - q[tail - 1]);
            ans += now;
        }
        return ans;
    }
    int main() {
        int T;
        scanf("%d",&T);
        while(T--) {
            scanf("%d",&n);
            for(int i = 1;i <= n;++i) scanf("%lld",&a[i]);
            ll ans = 0;
            ans += work();
            for(int i = 1;i <= n;++i) a[i] = -a[i];
            ans += work();
            cout<<ans<<endl;
        }
        return 0;
    }
    

    一言

    修行要有耐性,要能甘于淡泊,乐于寂寞。

  • 相关阅读:
    R-FCN、SSD、YOLO2、faster-rcnn和labelImg实验笔记
    yolov3的anchor机制与损失函数详解
    CV资料推荐
    测试用例设计方法总结
    测试需求分析
    bug生命周期
    linux命令一
    linux 命令二
    linux 命令三
    mysql数据库和禅道安装
  • 原文地址:https://www.cnblogs.com/wxyww/p/9896332.html
Copyright © 2020-2023  润新知