题目描述

One day, Snuke was given a permutation of length N, a1,a2,…,aN, from his friend.

Find the following:

Constraints
1≤N≤200,000
(a1,a2,…,aN) is a permutation of (1,2,…,N).

输入

The input is given from Standard Input in the following format:
N
a1 a2 … aN

输出

Print the answer.
Note that the answer may not fit into a 32-bit integer.

样例输入

3
2 1 3

样例输出

9

这是一道单调栈
单调栈的总结:https://blog.csdn.net/wubaizhe/article/details/70136174
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5+;
struct node
{
ll pre;
ll net;
ll num;
};
stack<node>sp;
ll s[maxn];
int main()
{
ios::sync_with_stdio(false);
int n;
cin>>n;
for(int i=;i<=n;i++) cin>>s[i];
ll ans=;
node fr,to;
fr.net=fr.pre=;
fr.num=s[];
sp.push(fr);
for(int i=;i<=n;i++){
to.num=s[i];
to.pre=to.net=;
while(!sp.empty()&&to.num<=sp.top().num){
fr=sp.top();
sp.pop();
if(!sp.empty()) sp.top().net+=fr.net;
to.pre+=fr.pre;
ans+=fr.pre*fr.num*fr.net;
}
sp.push(to);
}
while(!sp.empty())
{
fr=sp.top();
sp.pop();
if(!sp.empty()) sp.top().net+=fr.net;
ans+=fr.pre*fr.num*fr.net;
}
cout<<ans<<endl;
return ;
}

单调栈对求前后的有多少比他大/小很高效

1.首先这个运算要简化成:前面的大值*后面的大值(紧接的并且加上本身)

2.然后就是栈,极其烧脑┭┮﹏┭┮

Minimum Sum的更多相关文章

  1. 数学 - Whu 1603 - Minimum Sum

    Minimum Sum Problem's Link ------------------------------------------------------------------------- ...

  2. geeksforgeeks@ Minimum sum partition (Dynamic Programming)

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=166 Minimum sum partition Given an array, ...

  3. Minimum Sum(思维)

    Problem 1603 - Minimum Sum Time Limit: 2000MS   Memory Limit: 65536KB    Total Submit: 563  Accepted ...

  4. Minimum Sum LCM(uva10791+和最小的LCM+推理)

    L - Minimum Sum LCM Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submi ...

  5. UVA.10791 Minimum Sum LCM (唯一分解定理)

    UVA.10791 Minimum Sum LCM (唯一分解定理) 题意分析 也是利用唯一分解定理,但是要注意,分解的时候要循环(sqrt(num+1))次,并要对最后的num结果进行判断. 代码总 ...

  6. Minimum Sum of Array(map迭代器)

    You are given an array a consisting of n integers a1, ..., an. In one operation, you can choose 2 el ...

  7. HDU 3473 Minimum Sum(划分树)

    Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  8. Minimum Sum of Array(map)

    You are given an array a consisting of n integers a1, ..., an. In one operation, you can choose 2 el ...

  9. Whu 1603——Minimum Sum——————【单个元素贡献、滑窗】

    Problem 1603 - Minimum Sum Time Limit: 2000MS   Memory Limit: 65536KB   Total Submit: 623  Accepted: ...

  10. HDOJ 3473 Minimum Sum

    划分树,统计每层移到左边的数的和. Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

随机推荐

  1. Springboot前后端分离中,后端拦截器拦截后,前端没有对应的返回码可以判断

    项目登录流程如下 用户进入前端登录界面,输入账号密码等,输入完成之后前端发送请求到后端(拦截器不会拦截登录请求),后端验证账号密码等成功之后生成Token并存储到数据库,数据库中包含该Token过期时 ...

  2. Mac电脑如何彻底删除node

    之前本来想搭建一个hexo来写博客的,但是最后还是放弃,老老实实就在博客园和CSDN写博文了,这里记录一下怎么在Mac电脑下彻底删除node.js的方法 下面这个方法是我结合了网上好几个方法综合在一起 ...

  3. mysql插入文本文档及读取

    1.把本地的一个文件插入到数据库中,数据库字段用text保存 public static void main(String[] args) { PropKit.use(“pro.txt”);Druid ...

  4. mysql分组和排序操作

    分组.排序操作                                                                                         sele ...

  5. Windows下C extension not loaded for Word2Vec, training will be slow.解决方法

    在网上看了好多个博客,都没有很好解决,最后google.. 大概问题就是gensim库在安装时没有和其他一些包关联起来(可能是由于用pip安装的gensim导致这个问题),所以在用Word2Vec时没 ...

  6. 基于基因调控网络(Hopfield network)构建沃丁顿表观遗传景观

    基因调控网络的概念在之前已经简要介绍过:https://www.cnblogs.com/pear-linzhu/p/12313951.html 沃丁顿表观遗传景观(The Waddington's e ...

  7. Java连载71-二分查找和Arrays工具类

    一.二分法查找 1.二分法查找是建立在已经排序的基础之上的 2.程序分析是从下到大​排序. 3.这个数组中没有重复的元素​. package com.bjpowernode.java_learning ...

  8. BBS登录功能

    BBS登录功能 一.后端实现 1.实现验证码 from PIL import Image, ImageDraw, ImageFont import random from io import Byte ...

  9. 第一行代码近期bug及解决

    Android学习笔记(5)----启动 Theme.Dialog 主题的Activity时程序崩溃的解决办法https://www.cnblogs.com/dongling/p/6476308.ht ...

  10. 吴裕雄--天生自然 PYTHON3开发学习:OS 文件/目录方法

    import os, sys # 假定 /tmp/foo.txt 文件存在,并有读写权限 ret = os.access("/tmp/foo.txt", os.F_OK) prin ...