TOJ 4244: Sum
4244: Sum

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 108 indicating 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的更多相关文章
- TZOJ 4244 Sum(单调栈区间极差)
描述 Given a sequence, we define the seqence's value equals the difference between the largest element ...
- TOJ 2776 CD Making
TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性... 贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...
- 最小生成树 TOJ 4117 Happy tree friends
链接http://acm.tju.edu.cn/toj/showp4117.html 4117. Happy tree friends Time Limit: 1.0 Seconds Memo ...
- TOJ 4120 Zombies VS Plants
链接:http://acm.tju.edu.cn/toj/showp4120.html 4120. Zombies VS Plants Time Limit: 1.0 Seconds Memo ...
- TOJ 3365 ZOJ 3232 It's not Floyd Algorithm / 强连通分量
It's not Floyd Algorithm 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte 描述 When a directed grap ...
- TOJ 4008 The Leaf Eaters(容斥定理)
Description As we all know caterpillars love to eat leaves. Usually, a caterpillar sits on leaf, eat ...
- TOJ 4119 Split Equally
描述 Two companies cooperatively develop a project, but they don’t like working with one another. In o ...
- TOJ 4475: The Coolest Sub-matrix
4475: The Coolest Sub-matrix Time Limit(Common/Java):4000MS/12000MS Memory Limit:65536KByteTota ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
随机推荐
- Y2165终极分班考试题。
第一题答案:D 2.下面关于SQLServer中视图的说法错误的是:C 答案:视图是数据中存储的数据值得集合. 3.在JAVA中,关于日志记录工具log4j的描述错误的是:D 答案:log4j个输出级 ...
- bootstrap输入框组、导航和导航条
输入框组(input groups) 避免使用select 支持不好,使用输入框组 尺寸根据 input-group-lg input-group-sm来选择 <div class ...
- 解决java.lang.NoClassDefFoundError: javax/xml/rpc/service错误的方法
最近在做WebService项目,本地测试没有问题,打算部署到服务器上,但是部署后,访问时出现了如下图1的错误: 图1 图1报的是没有找到定义的类的错误.刷新页面有又出现了另外“新”的错误: 图2 根 ...
- 通过 Azure IoT 中心实现互联网设备数据的可视化分析
本课程主要介绍了如何 在Azure 平台上借助 Azure IoT 中心, Azure 流分析,Web 应用, Azure 数据库等服务快速构建收集处理并可视化来自设备的数据流的应用, 包括项目背景介 ...
- 系统妈Ghost Win10 64位快速安装版 V2016年2月
系统妈Ghost win10 64位快速安装版 V2016年2月,更新了最新系统补丁,升级系统版本号为2016年2月份.这款累积更新补丁会取代之前的版本.本系统还附带最常用的装机必备软件.QQ等. 系 ...
- codevs 1313 质因数分解
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题目描述 Description 已知正整数 n是两个不同的质数的乘积,试求出较大的那个质数 . 输入描述 I ...
- 生鲜o2o配送应用系统,包括Android源码+SSH带后台管理系统
前台功能划分 我的 登录 账户+密码 注册 订单管理 查看/删除(显示订单详情) 支付(提交订单) ...
- iOS图片目录批量复制到android图片目录
复制shell脚本 #!/bin/bash for i in `ls` do for imgname in `ls $i | grep '^WM.*'` do echo $imgname cp $i/ ...
- 设置windows status bar隐藏
info.plist View controller-based status bar appearance 为 NO CGContextSaveGState: invalid context 0x0 ...
- 模拟水题之unique两行AC
https://icpc.njust.edu.cn/Contest/749/A/ Description 小鱼喜欢吃糖果.他有两盒糖果,两盒糖果分别仅由小写字母组成的字符串s和字符串t构成.其中'a' ...