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 ...
随机推荐
- nginx重写常用写法
1.将http协议重写成https协议: (用户用http进行访问,但后端是https),则可添加80 http端口监听,然后进行https rewrite; server { listen ...
- nodejs(11)Express 中进行数据库操作
配置 MySql 数据库环境 mysql 第三方模块的介绍和基本配置 要安装操作数据库的第三方包npm i mysql -S 导入 包 const mysql = require('mysql') 创 ...
- ssh到ubuntu没颜色
ssh远程到ubuntu系统, 没有颜色. 原因是 .bashrc 配置没生效. $ echo '. $HOME/.bashrc' > ~/.profile
- 总结一些常用的训练 GANs 的方法
众所周知,GANs 的训练尤其困难,笔者自从跳入了 GANs 这个领域(坑),就一直在跟如何训练 GANs 做「对抗训练」,受启发于 ganhacks,并结合自己的经验记录总结了一些常用的训练 GAN ...
- normal equation(正规方程)
normal equation(正规方程) 正规方程是通过求解下面的方程来找出使得代价函数最小的参数的: \[ \frac{\partial}{\partial\theta_j}J\left(\the ...
- PAT Advanced 1074 Reversing Linked List (25) [链表]
题目 Given a constant K and a singly linked list L, you are supposed to reverse the links of every K e ...
- UML-如何画通信图?
1.链 2.消息 3.自身传递消息 4.消息顺序编号 5.有条件消息 6.互斥的有条件消息 7.循环或迭代 8.调用静态方法 9.多态 10.同步和异步调用
- vue-router中参数传递
VUE路由之间携带参数 今天在实现一个功能的时候遇到的问题,一个把组件a中的值传输到组件b中时,但是组件a和组件b之间通信的时候路由跳转了 猜想:路由跳转导致监听事件失败,(暂时理解为:当路由跳转后监 ...
- 洛谷 P1258 小车问题
题目传送门 解题思路: 首先,每个人都要做一次车,而且两个人要同时到达,这样才能使总时间最短. 那么,我们设起点为A,终点为B,小车先带甲开到C点后甲下车走到B点,同时小车掉头与已经走到D点的乙相向而 ...
- Linux inode 理解
inode 硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5KB).操作系统读取硬盘的时候,不会一个个扇区地读取,这样效率太低,而是一次性读取一 ...