4244: Sum  

Time Limit(Common/Java):3000MS/9000MS     Memory Limit:65536KByte
Total Submit: 63            Accepted:10

Description

Given a sequence, we define the seqence's value equals the difference between the largest element and the smallest element in the sequence. As an example, the value of sequence (3, 1, 7, 2) = 7-1 = 6.

Now, given a sequence S, output the sum of all value of consecutive subsequences.

Input

The first line has an integer N (2 ≤ N ≤ 300000) indicating the number of elements of the sequence. Then follows N lines, each line has a positive integer no larger than 10indicating an element of the sequence.

Output

Output the requested sum.

Sample Input

4
3
1
7
2

Sample Output

31

Hint

The consecutive subsequence of the sequence (3, 1, 7, 2) has:
(3, 1), (1, 7), (7, 2), (3, 1, 7), (1, 7, 2), (3, 1, 7, 2)

The sum of all values equals 2+6+5+6+6+6=31

这题看起来很简单啊,然后自然而然就想到了朴素的O(n^2)做法,然而TLE,仔细一想要不用dp做,保存当前最大值,可是不还是O(n^2),虽然循环次数少点,堆排序TLE同理。那天坐CF做完了无聊了就又在想这道题,想不出来就在群里问了问,大神给了我单调栈的做法和sort排序找位置的方法

sort大法来自010大神 点我获得代码

我的单调栈就是向左向右找到其可以左延伸右延伸的位置,排列组合就好了(左边包括他自己有n个,右侧包括他自己有m个,他的贡献就是(mn-1)个)

然后最大值来一次,最小值来一次,over

和普通的单调栈一样,这个的话就是检查扩展,可以扩展就去和前面那个值一个位置,其实求的就是最多扩展到哪里。一个值最多被访问两次,和单调栈一样都是2*n

单调栈也是,访问这个值,进队,可能还要出队。虽然是for套while但是复杂度还是n

#include <cstdio>
const int N=;
__int64 a[N];
int L[N],R[N];
int main() {
int n;
while(~scanf("%d",&n)) {
for(int i=; i<=n; i++) {
scanf("%I64d",&a[i]);
L[i]=i;
R[i]=i;
}
a[]=-;
a[n+]=-;
for(int i = ; i <= n; i++) {
while(a[i] <= a[L[i] - ])
L[i] = L[L[i] - ];
}
for(int i = n; i >= ; i--) {
while(a[i]< a[R[i] + ])
R[i] = R[R[i] + ];
}
__int64 sum=;
for(int i = ; i <= n; i++) {
sum-=a[i]*(i-L[i]+)*(R[i]-i+);
}
a[]=<<;
a[n+]=<<;
for(int i=; i<=n; i++) {
L[i]=i;
R[i]=i;
}
for(int i = ; i <= n; i++) {
while(a[i] >= a[L[i] - ])
L[i] = L[L[i] - ];
}
for(int i = n; i >= ; i--) {
while(a[i] > a[R[i] + ])
R[i] = R[R[i] + ];
}
for(int i = ; i <= n; i++) {
sum+=a[i]*(i-L[i]+)*(R[i]-i+);
}
printf("%I64d\n",sum);
}
return ;
}

TOJ 4244: Sum的更多相关文章

  1. TZOJ 4244 Sum(单调栈区间极差)

    描述 Given a sequence, we define the seqence's value equals the difference between the largest element ...

  2. TOJ 2776 CD Making

    TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性...  贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...

  3. 最小生成树 TOJ 4117 Happy tree friends

    链接http://acm.tju.edu.cn/toj/showp4117.html 4117.   Happy tree friends Time Limit: 1.0 Seconds   Memo ...

  4. TOJ 4120 Zombies VS Plants

    链接:http://acm.tju.edu.cn/toj/showp4120.html 4120.   Zombies VS Plants Time Limit: 1.0 Seconds   Memo ...

  5. TOJ 3365 ZOJ 3232 It's not Floyd Algorithm / 强连通分量

    It's not Floyd Algorithm 时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte   描述 When a directed grap ...

  6. TOJ 4008 The Leaf Eaters(容斥定理)

    Description As we all know caterpillars love to eat leaves. Usually, a caterpillar sits on leaf, eat ...

  7. TOJ 4119 Split Equally

    描述 Two companies cooperatively develop a project, but they don’t like working with one another. In o ...

  8. TOJ 4475: The Coolest Sub-matrix

    4475: The Coolest Sub-matrix  Time Limit(Common/Java):4000MS/12000MS     Memory Limit:65536KByteTota ...

  9. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

随机推荐

  1. js中函数的理解

     在JavaScript中,函数是一个极容易引起误解或引发歧义的数据类型,它可以是独立的函数类型,又可以作为对象的方法,也可以被称为类或构造器,还可以作为函数对象而存在等. 

  2. C语言中的fprintf函数详解

    fprintf 功能 传送格式化输出到一个文件中 用法 #include   stdio.h int fprintf( FILE *stream, const char *format,...); f ...

  3. 国家气象局提供的天气预报接口(完整Json接口)

    国家气象局提供的天气预报接口主要有三个,分别是:http://www.weather.com.cn/data/sk/101010100.htmlhttp://www.weather.com.cn/da ...

  4. 移动端:active伪类无效的解决方法

    :active伪类常用于设定点击状态下或其他被激活状态下一个链接的样式.最常用于锚点<a href="#">这种情况,一般主流浏览器下也支持其他元素,如button等. ...

  5. 苹果电脑macbook怎样强制关闭软件

    尝试快捷键Command+Q 选择当前处于界面最前端的应用,同时按住Command+Q退出程序,并不代表强制退出应用,主要用于一些假死的应用. 2 通过快捷键Command+option+Shift+ ...

  6. C05 C语言字符串和数组

    目录 数组 字符串 数组 概念 数组是有序数据的集合. 数组中的每一个元素属于同一个数据类型. 通过数组名和下标唯一确定数组中的元素. 一维数组的定义 语法格式 数据类型   数组名[常量表达式] 例 ...

  7. velocity生成静态页面代码

    首先需要必备的jar包: web.xml <!-- load velocity property --> <servlet> <servlet-name>veloc ...

  8. Luogu P3627 抢掠计划

    题目传送门 \(Tarjan\)缩点+SPFA最长路 #include<iostream> #include<cstdio> #include<cstring> # ...

  9. jQuery中Ajax事件beforesend及各参数含义

    Ajax会触发很多事件. 有两种事件,一种是局部事件,一种是全局事件: 局部事件:通过$.ajax来调用并且分配. $.ajax({ beforeSend: function(){ // Handle ...

  10. XML解析(一) DOM解析

    XML解析技术主要有三种: (1)DOM(Document Object Model)文档对象模型:是 W3C 组织推荐的解析XML 的一种方式,即官方的XML解析技术. (2)SAX(Simple ...