Topic Connection:
http://acm.hdu.edu.cn/showproblem.php?pid=1506
Main topic:
Given an array of an, how many elements are there in the interval with AI as the minimum?
Problem Solving Ideas:
Manually simulate a stack. The elements inside the stack are a monotonically non-descending sequence. When the new element AI needs to go into the stack, if the top element of the stack is larger than the AI, pop the stack top element until the top of the stack is not ai,ai into the stack.
Here you can guarantee that I is a rectangle that is high for the top element of the stack to extend the boundary back, and the edge of the matrix that is high for AI is recorded in the popup process.
1 //#include <bits/stdc++.h>2#include <iostream>3#include <algorithm>4#include <cstdio>5 using namespacestd;6 Const intMAXN =100005;7 #defineLL Long Long//to use ll, the data range is too large, int will overflow8 structnode9 {Ten LL x, index; One } STACK[MAXN]; A - intMain () - { the LL N; - while(SCANF ("%i64d", &N), N) - { -LL sum =0; + LL head, last, Num, m; -Head =0; +Last =-1; A for(LL i=0; i<=n; i++) at { - if(i = = N)//Add a 0 after the sequence to clear the elements inside the stack -num =0; - Else -scanf ("%i64d", &num); -m = i;//records the bounds of the current element's forward extension in while(Head<=last && stack[last].x>num) - { tosum = max (sum, (i-stack[last].index) *stack[last].x); +m =Stack[last].index; -Last--; the } *Stack[++last].index =m; $stack[last].x =num;Panax Notoginseng } -printf ("%i64d\n", sum); the } + return 0; A}
Summer Training Mania Brush Series--hdu 3506 largest Rectangle in a histogram (monotonic stack)