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

题意

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

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

#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. 零基础学到什么程度可以找一份web前端工作?

    能找到一份前端开发工作,首先你起码得是一个合格的初级前端工程师.那么,什么是初级前端工程师?初级前端工程师都会做些什么?这个问题需要分为以下几个方面来说: 一.对应岗位的工作职责初级前端,主要负责产品 ...

  2. JAVA 增删改查接口命名规范(dao层与 service 层

    开发时,有很多规范,这里写的是命名规范. Dao 接口命名   insert batchInsert selectOne selectById count selectList update dele ...

  3. mybatis 自学笔记

    MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.本页作为自学整理资料,信息来源网络,侵权速联,但大部份经过自己测试.使用说明:本人测试用编辑软件eclipse_st ...

  4. 查看deepin操作系统版本命令

    cat   /proc/version cat /etc/debian_version cat  /etc/os-release lsb_release -a uname -a uname -r sc ...

  5. List集合去重各种方式汇总

    package com.sb.test; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java. ...

  6. 如何在Mac和Windows PC之间无线共享文件

    有时候,我需要在Mac和PC之间无线共享文件.由于并非所有人都在使用macOS,因此无论是在办公室还是在家里,这种情况都会发生.尽管并非一帆风顺,但有一种无需任何第三方应用程序即可弥合差距的方法. 根 ...

  7. 消息驱动微服务:Spring Cloud Stream

    最近在学习Spring Cloud的知识,现将消息驱动微服务:Spring Cloud Stream 的相关知识笔记整理如下.[采用 oneNote格式排版]

  8. MATLAB应用专题part2-电力电子仿真技术

    有匪君子,如切如磋,如琢如磨. --<诗经·卫风·淇奥> 这篇博客知识我记录一下我在仿真学习中积累到的知识和遇到的坑. 第一部分:知识部分 1.为什么电阻与电感串联电路中电流的波形比电压的 ...

  9. 环绕通知(xml)

    1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

  10. Java8新特性一览表

    总览 forEach() method in Iterable interface(Iterable接口中的forEach()方法) default and static methods in Int ...