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

题意

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

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

#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;
}

[Agc005D/At2060] Minimum Sum - 单调栈的更多相关文章

  1. 【BZOJ4262】Sum 单调栈+线段树

    [BZOJ4262]Sum Description Input 第一行一个数 t,表示询问组数. 第一行一个数 t,表示询问组数. 接下来 t 行,每行四个数 l_1, r_1, l_2, r_2. ...

  2. Imbalanced Array CodeForces - 817D (思维+单调栈)

    You are given an array a consisting of n elements. The imbalance value of some subsegment of this ar ...

  3. Educational Codeforces Round 23 D. Imbalanced Array 单调栈

    D. Imbalanced Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. CF Mike and Feet (求连续区间内长度为i的最小值)单调栈

    Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. 2019牛客暑期多校训练营(第一场)A Equivalent Prefixes(单调栈/二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 Two arrays u and v each with m distinct elements ...

  6. POJ3250[USACO2006Nov]Bad Hair Day[单调栈]

    Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17774   Accepted: 6000 Des ...

  7. CodeForces 548D 单调栈

    Mike and Feet Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Subm ...

  8. Gym 101102D---Rectangles(单调栈)

    题目链接 http://codeforces.com/gym/101102/problem/D problem  description Given an R×C grid with each cel ...

  9. POJ2796Feel Good[单调栈]

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13376   Accepted: 3719 Case T ...

随机推荐

  1. 利用Bellman-Ford算法(有向图) 判断负环

    // 根据Bellman-Ford算法的原理 // 判断负环(算法的最大更新次数,应该是顶点数-1次) // 而如果存在负环,算法会一直更新下去 // 我们根据循环进行的次数,来判断负环 #inclu ...

  2. c# 异步编程总结

    异步编程前提 1.学委托 delegate 其中委托中的beginInvoke()和endInvoke()方法必须要会. 2.学习回调函数 (也可以不用,但是一般建议用回调函数中执行endinvoke ...

  3. Node中使用MongoDB

    简介 MongoDB 中文文档 MongoDB是一个介于关系数据库和非关系数据库(nosql)之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的. Mongoose 在Node中可以使用 Mo ...

  4. windows获取所有连接过的无线网密码

    一.打开命令行工具:win+R 输入cmd 回车进入: for /f "skip=9 tokens=1,2 delims=:" %i in ('netsh wlan show pr ...

  5. redis缓存优化

    redis缓存优化 一.问题 在Javaweb项目中,如果每次刷新,所有资源都重新从数据库中读取,这样每次效率会很低,在这里可以使用redis非关系型数据库,将一些不经常变化得资源加载进内存中.提高效 ...

  6. webpack安装jQuery报错

    使用webpack搭建项目,并使用了node下载了jQuery使用,使用命令行完成构建时发现报错了, ERROR in ./node_modules/jquery/lib/node-jquery.js ...

  7. 生成随机数(C++)

    // generate random number #include <iostream> #include <iomanip> #include <cstdlib> ...

  8. 破局AI落地难,数据标注行业需率先变革丨曼孚科技

    ​2019年,国内人工智能领域的投融资热情大幅降低,相当数量的AI企业彻底消失在了历史的长河中,“人工智能寒潮已至”甚至成为行业年度热词. 与前几年创业与投资热情齐头并进的盛况相比,近段时间的AI行业 ...

  9. 安装canvas

    本方法仅适用用于window系统 安装canvas需要当前工作环境拥有python环境,且只能适用python2.7版本,v3.x.x版本会造成系统报错 1.在管理员权限下 使用choco insta ...

  10. IDEA开发Maven构建SSM项目遇到的坑,action

    Request processing failed; nested exception is org.apache.ibatis.binding.BindingException: Invalid b ...