Minimum Sum
题目描述
Find the following:

Constraints
1≤N≤200,000
(a1,a2,…,aN) is a permutation of (1,2,…,N).
输入
N
a1 a2 … aN
输出
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的更多相关文章
- 数学 - Whu 1603 - Minimum Sum
Minimum Sum Problem's Link ------------------------------------------------------------------------- ...
- geeksforgeeks@ Minimum sum partition (Dynamic Programming)
http://www.practice.geeksforgeeks.org/problem-page.php?pid=166 Minimum sum partition Given an array, ...
- Minimum Sum(思维)
Problem 1603 - Minimum Sum Time Limit: 2000MS Memory Limit: 65536KB Total Submit: 563 Accepted ...
- Minimum Sum LCM(uva10791+和最小的LCM+推理)
L - Minimum Sum LCM Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submi ...
- UVA.10791 Minimum Sum LCM (唯一分解定理)
UVA.10791 Minimum Sum LCM (唯一分解定理) 题意分析 也是利用唯一分解定理,但是要注意,分解的时候要循环(sqrt(num+1))次,并要对最后的num结果进行判断. 代码总 ...
- 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 ...
- HDU 3473 Minimum Sum(划分树)
Minimum Sum Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 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 ...
- Whu 1603——Minimum Sum——————【单个元素贡献、滑窗】
Problem 1603 - Minimum Sum Time Limit: 2000MS Memory Limit: 65536KB Total Submit: 623 Accepted: ...
- HDOJ 3473 Minimum Sum
划分树,统计每层移到左边的数的和. Minimum Sum Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/32768 K ...
随机推荐
- Vue动态添加v-model绑定及获取其返回数据
从数据库拿到的动态数据绑定到页面对应的v-model或者v-bind上,并且根据对页的操作获取到返回的值: 1.首先在data里定义一个数据 timeTip 为一个空数组 data () { retu ...
- maven镜像仓库
国内连接maven官方的仓库更新依赖库,网速一般很慢,收集一些国内快速的maven仓库镜像以备用. ====================国内OSChina提供的镜像,非常不错=========== ...
- Django模型基础——(二)
上篇博客主要讲了django中对数据库的增删改查,下面深入再讲解下对数据库的操作. 常用的查询方法 下面以表名为User为例 User.object.first() :返回表中第一条数据 User.o ...
- [Python]distributed 1.21.8 requires msgpack, which is not installed.
个人博客原文地址:http://www.bearoom.xyz/2019/08/24/python-devolop-en-msgpack-problem/ 在安装tensorflow的时候,出现了这个 ...
- Linux系统提示无法获得锁
这种情况出现主要是因为软件更新或者安装时出现错误. 删除掉两个临时文件即可 sudo rm /var/lib/dpkg/lock sudo rm /var/cache/apt/archive/lock ...
- Codeforces Round #621 (Div. 1 + Div. 2)D dij(思维)
题:https://codeforces.com/contest/1307/problem/D 题意:给定无向图,n为点,m为边.在给个k,为特殊点的数目,题目要求在这些特殊点上连一条边,让新图最短路 ...
- Scrapy连接到各类数据库(SQLite,Mysql,Mongodb,Redis)
如何使用scrapy连接到(SQLite,Mysql,Mongodb,Redis)数据库,并把爬取的数据存储到相应的数据库中. 一.SQLite 1.修改pipelines.py文件加入如下代码 # ...
- nginx常用模块(一)
1.Nginx目录索引 1.1Nginx默认是不允许列出整个目录浏览下载.Syntax: autoindex on | off;Default: autoindex off;Context: http ...
- switch-case的用法
case 值1: 表达式的值和 值1匹配上了,需要执行的代码; break; case 值2: 表达式的值和 值2匹配上了,需要执行的代码; break; case 值3: 表达式的值和 值3匹配上了 ...
- 是时候写个自己的dialog了
组件下载地址:http://pan.baidu.com/s/1pJFVfej 最近做的项目需要用到对话框,但是原生的弹出框你是知道的.如果有时间,还是自己尝试一下,也是可以的. 一个简单图 里面的输入 ...