fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. #define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie()
  4.  
  5.  
  6.  
  7. using namespace std;
  8. typedef long long ll;
  9.  
  10.  
  11. void File(){
  12. #ifndef ONLINE_JUDGE
  13. freopen("input.txt", "r", stdin);
  14. freopen("output.txt", "w", stdout);
  15. #endif
  16. }
  17.  
  18. ll largestRectangleArea(vector <ll> heights) {
  19. int n = heights.size();
  20. stack <int> st;
  21.  
  22. vector<ll> leftsmall(n), rightsmall(n);
  23.  
  24. for (int i = 0; i < n; i++) {
  25.  
  26. while (!st.empty() && heights[st.top()] >= heights[i]){
  27. st.pop();
  28. }
  29.  
  30.  
  31. leftsmall[i]=((st.empty())? 0: st.top() + 1);
  32.  
  33. st.push(i);
  34. }
  35.  
  36.  
  37. while(!st.empty()){
  38. st.pop();
  39. }
  40.  
  41. for (int i = n - 1; i >= 0; i--) {
  42. while (!st.empty() && heights[st.top()] >= heights[i]){
  43. st.pop();
  44. }
  45.  
  46. rightsmall[i] = ((st.empty())? n - 1: st.top() - 1);
  47.  
  48. st.push(i);
  49. }
  50.  
  51. ll mxarea = 0;
  52. for (int i = 0; i < n; i++){
  53. mxarea = max(mxarea, heights[i] * (rightsmall[i] - leftsmall[i] + 1));
  54. }
  55. return mxarea;
  56. }
  57.  
  58. int solve() {
  59. int n;
  60. cin>>n;
  61. vector<ll> v(n);
  62. for(int i=0;i<n;i++){
  63. cin>>v[i];
  64. }
  65. cout<<largestRectangleArea(v)<<endl;
  66.  
  67. return 0;
  68. }
  69.  
  70. int main() {
  71. fastio();
  72. File();
  73. int32_t t=1;
  74. cin>>t;
  75. for(auto i=t;i--;){
  76. solve();
  77. }
  78. }
  79.  
Success #stdin #stdout 0.01s 5276KB
stdin
2
7
2 1 4 5 1 3 3
4
1000 1000 1000 1000
stdout
8
4000