[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 }
View Code

 

posted @ 2015-04-12 06:07  jklongint  阅读(199)  评论(0编辑  收藏  举报