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 ...
随机推荐
- 求1+2+3+…..+n
[问题]求1+2+3+…+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). [思路]由于题目好多运算符不能用,我们只有想到使用递 ...
- 连接mysql的各种方式
mysql连接操作是客户端进程与mysql数据库实例进程进行通信.从程序设计角度来说,属于进程通信,常用进程通信包括: 管道.Tcp/Ip 套接字.UNIX域套接字. 1.TCP/IP (1)使用最多 ...
- A4纸网页打印
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- idea拉取git项目并创建为maven项目(新创建github项目)
0 环境 系统环境:win10 编辑器:idea 1 正文 1 clone项目 跟着提示yes 下一步 2 在根节点添加pom.xml(maven) <?xml version="1. ...
- Django框架(十一):模板介绍、模板语言、模板继承、HTML转义
1. 模板介绍 1.1 模板的功能 产生html,控制页面上展示的内容.模板文件不仅仅是一个html文件. 模板文件包含两部分内容: 静态内容:css.js.html. 动态内容:用于动态去产生一些页 ...
- Fib数列问题(项数很大)
用fib(n)表示斐波那契数列的第n项,现在要求你求fib(n) mod m.fib(1)= 1, fib(2)= 1. 输入格式 输入2个整数n(1≤n≤1018), m(2≤m≤10000000) ...
- HTTP Error 500.30 - ANCM In-Process Start Failure错误。.NET Core
调试.NET Core项目.出现了以下的错误.学网上搞了好久IIS没卵用.然后根据微软的提示,解决了问题. 解决方法: 1. 目标平台换成Any CPU 2.点击工具-获取工具和功能,把下面这个II ...
- 03 Mybatis:05.使用Mybatis完成CRUD
mybatis框架:共四天 明确:我们在实际开发中,都是越简便越好,所以都是采用不写dao实现类的方式.不管使用XML还是注解配置. 第二天:mybatis基本使用 mybatis的单表crud操作 ...
- Ubuntu python多个版本管理
1.查看python有哪些版本使用命令 whereis python 如图: 2.这么多版本如何切换呢 使用 sudo update-alternatives --install <link&g ...
- java 用condition&reentrylock实现生产者消费者
package com.lb; import java.util.ArrayList; import java.util.List; import java.util.concurrent.locks ...