Test instructions: http://acm.hdu.edu.cn/showproblem.php?pid=1506 look at the picture at a glance. Two-direction monotonic queue maintenance.
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>Ten#include <vector> One#include <ctime> A#include <cctype> -#include <Set> - the using namespacestd; - - #defineMem0 (a) memset (a, 0, sizeof (a)) - #defineLson L, M, RT << 1 + #defineRson m + 1, R, RT << 1 | 1 - #definedefine_m int m = (L + r) >> 1 + #defineRep (A, b) for (int a = 0; a < b; a++) A #defineLowbit (x) ((x) & (-(x))) at #defineCONSTRUCTINT4 (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) {} - #defineCONSTRUCTINT3 (name, a, B, c) name (int a = 0, int b = 0, int c = 0): A (a), B (b), C (c) {} - #defineConstructInt2 (name, a, b) name (int a = 0, int b = 0): A (a), B (b) {} - -typedefDoubledb; -typedefLong LongLL; intypedef pair<int,int>PII; -typedef multiset<int>MSI; totypedef multiset<int>:: iterator msii; +typedefSet<int>si; -typedefSet<int>:: iterator sii; the * Const intdx[8] = {1,0, -1,0,1,1, -1, -1}; $ Const intdy[8] = {0, -1,0,1, -1,1,1, -1};Panax Notoginseng Const intMAXN = 1e6 +7; - Const intMAXM = 1e5 +7; the Const intMD = 1e9 +7; + Const intINF = 1e9 +7; A thetemplate<classT>structmonotonequeue{ +Deque<t>Q; -Monotonequeue<t>() {q.clear ();} $ voidClear () {q.clear ();} $ BOOLEmpty () {returnq.empty ();} - voidAdd_back (T x) { while(! Q.empty () &&! (Q.back () <x)) Q.pop_back (); Q.push_back (x); } - voidPop_front () {Q.pop_front ();} theT Back2 () {return* (Q.end ()-2); } -T Front () {returnQ.front ();}Wuyi }; the - structPair1 { Wu intVal, pos; - BOOL operator< (ConstPair1 &a)Const { About returnVal <A.val; $ } - ConstructInt2 (Pair1, Val, pos); - }; - AMonotonequeue<pair1>UQ; + the inta[100010], l[100010]; - $ intMain () { the //freopen ("In.txt", "R", stdin); the intN; the while(Cin >>N, N) { the uq.clear (); -Uq.add_back (Pair1 (-1, -1)); in for(inti =0; I < n; i++) { thescanf"%d", A +i); the Uq.add_back (Pair1 (a[i], i)); AboutL[i] = Uq.back2 (). Pos +1; the } the uq.clear (); theUq.add_back (Pair1 (-1, N)); +LL ans =0; - for(inti = n-1; I >=0; i--) { the Uq.add_back (Pair1 (a[i], i));Bayians = max (ans, (LL) a[i] * (Uq.back2 (). POS-l[i])); the } thecout << ans <<Endl; - } - return 0; the}View Code
[hdu1506] monotone queue (stack)