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 ...
随机推荐
- Java自学-集合框架 聚合操作
聚合操作 步骤 1 : 聚合操作 JDK8之后,引入了对集合的聚合操作,可以非常容易的遍历,筛选,比较集合中的元素. 像这样: String name =heros .stream() .sorted ...
- mysql float 这个大坑
以后高精度的数据不要用这个字段 今天同事反应 应该是17390.7的数据 结果展示17390.6992 找了半天问题在哪 后来决定先不管 手动现在数据库改数据 结果手动改也改不过来 于是能确定 ...
- Unix网络编程学习 < 一 >
#include "unp.h" int main(int argc , char**argv) { int sockfd , n; //sockfd套接字描述符 ]; struc ...
- IDEA常用技巧以及快捷键总结
一.常用快捷键 快捷键 描述 Ctrl+o 复写父类方法 Alt+7 查看类所有方法实现 Ctrl+Alt+H 方法调用链
- 解决 springweb Filter 读取request body miss body
package com.lb.demo.listener; import java.io.BufferedReader; import java.io.ByteArrayInputStream; im ...
- es和数据库关系对比
es类比传统关系型数据库: Relational DB -> Databases -> Tables -> Rows -> Columns Elasticsearch -& ...
- MyBatis项目报错:The server time zone value '�й�' is unrecognized or represents more than one time zone
问题来源:在部署Spring + MyBatis项目的时候,运行时报错The server time zone value '�й�' is unrecognized or represents m ...
- springboot整合redis简单示例
一.项目架构 二.项目内容 1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...
- 在CentOS/RHEL 7下修改网卡名为 eth0 形式
为了统一企业中的多版本系统共存的环境,这里将网卡名称设置为 eth* 的形式,不使用CentOS/RHEL 7默认的特殊网卡命名规则.所以需要在安装初始的时候,需要增加内核参数. 在启动界面,按 TA ...
- JavaScript数组去重(5种方法)
// 数组去重 let arr = ['a', 'b', 'b', 1, 1, 'true', true, true, NaN, NaN, 'NaN', undefined, undefined, n ...