[hdu1506]单调队列(栈)

题意:http://acm.hdu.edu.cn/showproblem.php?pid=1506看图一目了然。两个方向单调队列维护下。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <map>
 7 #include <queue>
 8 #include <deque>
 9 #include <cmath>
10 #include <vector>
11 #include <ctime>
12 #include <cctype>
13 #include <set>
14
15 using namespace std;
16
17 #define mem0(a) memset(a, 0, sizeof(a))
18 #define lson l, m, rt << 1
19 #define rson m + 1, r, rt << 1 | 1
20 #define define_m int m = (l + r) >> 1
21 #define Rep(a, b) for(int a = 0; a < b; a++)
22 #define lowbit(x) ((x) & (-(x)))
23 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
24 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
25 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
26
27 typedef double db;
28 typedef long long LL;
29 typedef pair<int, int> pii;
30 typedef multiset<int> msi;
31 typedef multiset<int>::iterator msii;
32 typedef set<int> si;
33 typedef set<int>::iterator sii;
34
35 const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
36 const int dy[8] = {0, -1, 0, 1, -1, 1, 1, -1};
37 const int maxn = 1e6 + 7;
38 const int maxm = 1e5 + 7;
39 const int MD = 1e9 +7;
40 const int INF = 1e9 + 7;
41
42 template<class T> struct MonotoneQueue{
43     deque<T> Q;
44     MonotoneQueue<T>() { Q.clear(); }
45     void clear() { Q.clear(); }
46     bool empty() { return Q.empty(); }
47     void add_back(T x) { while (!Q.empty() && !(Q.back() < x)) Q.pop_back(); Q.push_back(x); }
48     void pop_front() { Q.pop_front(); }
49     T back2() { return *(Q.end() - 2); }
50     T front() { return Q.front(); }
51 };
52
53 struct pair1 {
54     int val, pos;
55     bool operator < (const pair1 &a) const {
56         return val < a.val;
57     }
58     constructInt2(pair1, val, pos);
59 };
60
61 MonotoneQueue<pair1> UQ;
62
63 int a[100010], L[100010];
64
65 int main() {
66     //freopen("in.txt", "r", stdin);
67     int n;
68     while (cin >> n, n) {
69         UQ.clear();
70         UQ.add_back(pair1(-1, -1));
71         for (int i = 0; i < n; i++) {
72             scanf("%d", a + i);
73             UQ.add_back(pair1(a[i], i));
74             L[i] = UQ.back2().pos + 1;
75         }
76         UQ.clear();
77         UQ.add_back(pair1(-1, n));
78         LL ans = 0;
79         for (int i = n - 1; i >= 0; i--) {
80             UQ.add_back(pair1(a[i], i));
81             ans = max(ans, (LL)a[i] * (UQ.back2().pos - L[i]));
82         }
83         cout << ans << endl;
84     }
85     return 0;
86 }

时间: 2024-08-02 05:35:10

[hdu1506]单调队列(栈)的相关文章

【NOIP数据结构专项】单调队列单调栈

[洛谷P1901 ]发射站 http://www.luogu.org/problem/show?pid=1901 题目描述 某地有 N 个能量发射站排成一行,每个发射站 i 都有不相同的高度 Hi,并能向两边(当 然两端的只能向一边)同时发射能量值为 Vi 的能量,并且发出的能量只被两边最近的且比 它高的发射站接收. 显然,每个发射站发来的能量有可能被 0 或 1 或 2 个其他发射站所接受,特别是为了安 全,每个发射站接收到的能量总和是我们很关心的问题.由于数据很多,现只需要你帮忙计 算出接收

小结:单调栈 &amp; 单调队列

概要: 对于维护信息具有单调性的性质或者问题可以转化为具有单调性质的模型的题,我们可以考虑用单调栈或单调队列. 技巧及注意: 技巧很多,只要能将问题转化为单调性问题,就好解决了. 当维护固定长度的单调区间,我们考虑用单调队列,如[BZOJ]3314: [Usaco2013 Nov]Crowded Cows(单调队列) 单调栈维护长度时要进行及时更新,例如:[BZOJ]3039: 玉蟾宫(DP/单调栈) 假设完美状态后再进行减法原理,例如:[BZOJ]1628 && 1683: [Usaco

数据结构之单调栈单调队列模板

单调栈 int rear=0; for(int st=1;st<=N;st++) { while(rear>0&&H[que[rear]]>=H[st]) --rear; if(rear==0) le[st]=0; else le[st]=que[rear]; que[++rear]=st; } 单调队列 int que[maxn],elem[maxn]; int front=1,rear=0; for(int i=1;i<K;i++) { while(rear&g

浅谈单调队列、单调栈

       初谈这个话题,相信许多人会有一种似有所悟,但又不敢确定的感觉.没错,这正是因为其中"单调"一词的存在,所谓单调是什么,学过函数的people都知道单调函数或者函数的单调性,直白一点说单调就是一直增或一直减.例如:1,3,5,9就是一个单调增数列,数列中不存在后一个数比前一个数小的现象.那么同样,在这里谈到的话题也有类似特点.        先说一下单调队列吧!      单调队列,就是一个符合单调性质的队列,它同时具有单调的性质以及队列的性质.他在编程中使用频率不高,但却

小Z爱序列(NOIP信(sang)心(bin)赛)From Fall_Dream(粗制单调队列&amp;单调栈的算法解析)

原题: 小Z最擅长解决序列问题啦,什么最长公共上升然后下降然后上升的子序列,小Z都是轻松解决的呢. 但是小Z不擅长出序列问题啊,所以它给了你一道签到题. 给定一个n个数的序列ai,你要求出满足下述条件的点对的数量. 假设点对是(i , j),max(l,r)是[l,r]当中最大的ai的值. 这个点对满足条件当且仅当i+1<j 且 ai < max(i+1,j-1) < aj 为了简单,保证输入的是一个1-n的排列.相信你已经会做了吧? 输入/输出格式 输入数据第一行有一个数字n,然后第二

hdu4193---Non-negative Partial Sums(单调队列 or 单调栈)

Problem Description You are given a sequence of n numbers a0,-, an-1. A cyclic shift by k positions (0<=k<=n-1) results in the following sequence: ak ak+1,-, an-1, a0, a1,-, ak-1. How many of the n cyclic shifts satisfy the condition that the sum of

单调队列,单调栈相关

说起这个话题,应该很多人会有一种似有所悟,但又不敢确定的感觉. (我差不多就是那样) 没错,这正是因为其中“单调”一词的存在. 那么单调是什么? 学过函数的人都知道单调函数或者函数的单调性吧 其实直白一点说单调,就是一直增或一直减. eg:1,3,5,9就是一个单调增数列,数列中不存在后一个数比前一个数小的现象. 那么同样,在这里谈到的话题也有类似特点. (一)单调队列 其实就是一个符合单调性质的队列,但它同时具有单调的性质以及队列的性质. 使用频率不算高,但却占有至关重要的地位.它的作用很简单

单调栈&amp;单调队列入门

单调队列是什么呢?可以直接从问题开始来展开. Poj 2823 给定一个数列,从左至右输出每个长度为m的数列段内的最小数和最大数. 数列长度:\(N <=10^6 ,m<=N\) 解法① 很直观的一种解法,那就是从数列的开头,将窗放上去,然后找到这最开始的k个数的最大值,然后窗最后移一个单元,继续找到k个数中的最大值. 这种方法每求一个f(i),都要进行k-1次的比较,复杂度为$ O(Nk) $. 显然,如果暴力时间复杂度为 $ O(Nm) $ 不超时就怪了. 解法② 还有一种想法是维护一个B

单调队列单调栈

单调队列单调栈 Tags:数据结构 更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1041449 一.概述 单调队列单调栈是很基础的数据结构,常用来优化一些东西比如说优化DP 那么大概意思就是在栈或队列中按照某关键字单调维护信息,从而实现一些功能 其实很久之前接触过单调队列和单调栈,但一直没有刷题,趁这两天被LCT弄晕的时候复习下这些 先看题 二.题单 普及- [x] P1886 滑动窗口 https://www.luogu.org/problemnew/