#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
multiset<long long>mst;
long long a[100007];
int main(){
long long n;
cin>>n;
long long x;
for(long long i=0;i<n;++i){
cin>>x;
a[x]=i;
}
mst.insert(-1);
mst.insert(-1);
mst.insert(n);
mst.insert(n);
long long ans=0;
set<long long>::iterator pos,nex,pre,pree;
for(long long i=n;i;--i){//倒着插,使得集合内除了初始插入的两个-1和两个n其余的位置它们的值都比后插入的值大
pos=mst.upper_bound(a[i]);//比当前i的值大的下一个值的位置
nex=next(pos);//比pos大的下一个值的位置
pre=prev(pos);//比当前i的值小的上一个值的位置
pree=prev(pre);//比pre小的上一个值的位置
ans+=i*((*nex-*pos)*(a[i]-*pre)+(*pos-a[i])*(*pre-*pree));//区间内只有一个值比当前i值大
mst.insert(a[i]);//将当前值的位置插入
}
cout<<ans;
return 0;
}

Atcoder Beginner Contest 140E(多重集,思维)的更多相关文章

  1. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  2. AtCoder Beginner Contest 052

    没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...

  3. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  4. AtCoder Beginner Contest 136

    AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...

  5. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

  6. AtCoder Beginner Contest 076

    A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...

  7. AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】

    AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...

  8. AtCoder Beginner Contest 064 D - Insertion

    AtCoder Beginner Contest 064 D - Insertion Problem Statement You are given a string S of length N co ...

  9. AtCoder Beginner Contest 075 D - Axis-Parallel Rectangle【暴力】

    AtCoder Beginner Contest 075 D - Axis-Parallel Rectangle 我要崩溃,当时还以为是需要什么离散化的,原来是暴力,特么五层循环....我自己写怎么都 ...

随机推荐

  1. laravel执行数据库迁移的过程中出现Illuminate\Database\QueryException : SQLSTATE[HY000] [2002] Operation timed out (SQL: select * from information_schema.tables where table_schema = shop and table_name = migrations

    向customers表添加字段phone php artisan make:migration add_phone_to_customers_table 问题: 解决方法: 将DB_HOST配置项修改 ...

  2. 安装nodejs时提示Leaving directory

    在按照标准的编译命令./configure =>make =>make install 在make的时候发生错误: ../deps/v8/src/base/platform/mutex.h ...

  3. type和object详解

    python中的type和object详解   关于这篇博客 这篇博客主要描述Python的新风格对象(new-style objects),如下: <type 'type'>和<t ...

  4. STM32F030 启用内部晶振并配置系统时钟为48M

    在文件 system_stm32f0xx.c 里的函数 static void SetSysClock(void) { if (HSEStatus == (uint32_t)0x01) // 存在外部 ...

  5. lc 0227

    目录 ✅ 1189. "气球" 的最大数量 描述 解答 cpp py improved py ✅ 784. 字母大小写全排列 描述 解答 c: backTrace !! power ...

  6. Json日期格式化,出去返回的T

    第一种办法:前端JS转换: //格式化显示json日期格式 function showDate(jsonDate) { var date = new Date(jsonDate); var forma ...

  7. oracle导入失败,超出长度

    oracle导入失败,超出长度 出现ORA-12899,是字符集引起的,中文在UTF-8中占3个字节,ZHS16GBK中占2个字节,而源dmp文件字符集是ZHS16GBK库里倒出来的数据,现在要导入到 ...

  8. Dubbo教程:入门到实战

    Dubbox简介 Dubbox 是一个分布式服务框架,其前身是阿里巴巴开源项目Dubbo ,被国内电商及互联网项目中使用,后期阿里巴巴停止了该项目的维护,当当网便在Dubbo基础上进行优化,并继续维护 ...

  9. 树莓派Ubuntu Mate 16.04 修改为国内更新源

    收藏:https://blog.csdn.net/wang_shuai_ww/article/details/80386708 更换步骤以root身份打开 /etc/apt/sources.list ...

  10. 洛谷P1164小A点菜(01背包)

    题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家……餐馆,很低端的那种. uim指着墙上的价目表(太低级了没有菜单),说:“随便点”. 题目描述 不过uim由于买了一些辅(e ...