Codeforces Round #333 (Div. 1)--B. Lipshitz Sequence 单调栈
题意:n个点, 坐标已知,其中横坐标为为1~n。 求区间[l, r] 的所有子区间内斜率最大值的和。
首先要知道,[l, r]区间内最大的斜率必然是相邻的两个点构成的。
然后问题就变成了求区间[l, r]内所有子区间最大值的和。
这个问题可以利用单调栈来做。
每次找到当前点左面第一个大于当前值的点, 然后更新答案。 姿势很多。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.Scanner; public class Main {
static Scanner cin = new Scanner(new BufferedInputStream(System.in));
static PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out));
static final int maxn = 100005;
public static void main(String[] args) {
int []height = new int[maxn];
while (cin.hasNext()){
int n = cin.nextInt();
int q = cin.nextInt();
height[0] = 0;
for (int i = 1; i <= n; i++){
height[i] = cin.nextInt();
height[i-1] = Math.abs(height[i]-height[i-1]);
}
int []stack = new int[maxn];
int top = -1;
for (int i = 0; i < q; i++){
int l = cin.nextInt();
int r = cin.nextInt();
long res = 0, cur = 0;
top = -1;
for (int j = l; j < r; j++){
while (top >= 0 && height[stack[top]] <= height[j]){
cur -= 1L * height[stack[top]] * (stack[top] - (top==0 ? l-1 : stack[top-1]));
top--;
}
if (top >= 0){
cur += 1L* (j - stack[top]) * height[j];
}else{
cur += 1L * (j - l + 1) * height[j];
}
stack[++top] = j;
res += cur;
} cout.println(res);
}
cout.flush();
}
} }
Codeforces Round #333 (Div. 1)--B. Lipshitz Sequence 单调栈的更多相关文章
- Codeforces Round #333 (Div. 1) B. Lipshitz Sequence 倍增 二分
B. Lipshitz Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/601/ ...
- Codeforces Round #541 (Div. 2) G dp + 思维 + 单调栈 or 链表 (连锁反应)
https://codeforces.com/contest/1131/problem/G 题意 给你一排m个的骨牌(m<=1e7),每块之间相距1,每块高h[i],推倒代价c[i],假如\(a ...
- Codeforces Round #333 (Div. 2)
水 A - Two Bases 水题,但是pow的精度不高,应该是转换成long long精度丢失了干脆直接double就可以了.被hack掉了.用long long能存的下 #include < ...
- Codeforces Round #333 (Div. 2) B. Approximating a Constant Range st 二分
B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...
- Codeforces Round #353 (Div. 2) A. Infinite Sequence 水题
A. Infinite Sequence 题目连接: http://www.codeforces.com/contest/675/problem/A Description Vasya likes e ...
- Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)
链接: https://codeforces.com/contest/1265/problem/D 题意: An integer sequence is called beautiful if the ...
- Codeforces Round #333 (Div. 2) B. Approximating a Constant Range
B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...
- Codeforces Round #353 (Div. 2) A. Infinite Sequence
Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its fi ...
- Codeforces Round #333 (Div. 1) C. Kleofáš and the n-thlon 树状数组优化dp
C. Kleofáš and the n-thlon Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
随机推荐
- PHP 一维数组排序
一.按照键值从低到高排序,并赋予新的键名 1 函数:sort( array &array[, int sort_flags]) 2 参数: (1)&array : 要排序的一维数组, ...
- xargs rm -rf 与 -exec rm
# find ./ -exec rm {} \; # find ./ | xargs rm -rf 两者都可以把find命令查找到的结果删除,其区别简单的说是前者是把find发现的结果一次性传给exe ...
- 层模型--绝对定位(position:absolute)
如果想为元素设置层模型中的绝对定位,需要设置position:absolute(表示绝对定位),这条语句的作用将元素从文档流中拖出来,然后使用left.right.top.bottom属性相对于其最接 ...
- 添加Appicon的方法
1.将设计好的图片,拖拽到Groups&Files的Resources目录下: 2.修改Resources目录下的“工程名-info.plist”文件 3.修改该文件的Iconfile属性,填 ...
- VisualStudio2013内置SQLServer入门
最近做项目老大要求用到sqlserver,但是这项目的数据库只是本地演示用并不复杂,于是决定试试VisualStudio2013内置的SQLServer.对于这个东西的了解并没有多少,然后项目初学习的 ...
- 非注解SpringMVC
<!-- SpringMVC前端控制器 --> <servlet> <servlet-name>springmvc</servlet-name> < ...
- SGU 155.Cartesian Tree
时间限制:0.25s 空间限制:6M 题意: 给出n(n< 50000)个含双关键字(key,val)的节点,构造一颗树使该树,按key值是一颗二分查找树,按val值是一个小根堆. Soluti ...
- Oracle IN 传递字符串参数查询失效
在写存储过程中有如下代码: FOR a IN ( SELECT a.svo_no,a.AUDIT_NO,a.order_id FROM TT_PI_MODEL_REL a ) LOOP SELECT ...
- git学习利器:《Git Pro》中文版
Git书籍有<版本控制之道git>,但是很一般.强烈推荐<Git Pro>中文版! 很多开源软件的教程也是免费开源的在线阅读的. <Git Pro>中文版在线阅读h ...
- js 实现tab选项卡
最近一直在研究js 如果不及时复习的话前边学到的东西很快就会忘掉,所以把前段时间的一个简单的tab选项卡的思路写出来也算复习了一下吧, 第一步:先把布局写出来: <div id="d ...