本文共 2389 字,大约阅读时间需要 7 分钟。
题意:http://acm.hdu.edu.cn/showproblem.php?pid=1506看图一目了然。两个方向单调队列维护下。
1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 14 15 using namespace std;16 17 #define mem0(a) memset(a, 0, sizeof(a))18 #define lson l, m, rt << 119 #define rson m + 1, r, rt << 1 | 120 #define define_m int m = (l + r) >> 121 #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 pii;30 typedef multiset msi;31 typedef multiset ::iterator msii;32 typedef set si;33 typedef set ::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 struct MonotoneQueue{43 deque Q;44 MonotoneQueue () { 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 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 }
转载于:https://www.cnblogs.com/jklongint/p/4418995.html