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. ll largestRectangleArea(vector <ll> heights) {
  18. stack<int> st;
  19.  
  20. int n=heights.size(),i=n-1;
  21.  
  22. ll area,mxarea=-1,top;
  23.  
  24.  
  25.  
  26. while (i >= 0) {
  27. if (st.empty() || heights[i] >= heights[st.top()]) {
  28. st.push(i--);
  29. } else {
  30. top = st.top();
  31. st.pop();
  32. area = heights[top] * (st.empty() ? n-i-1 : st.top()-i-1);
  33. mxarea = max(mxarea, area);
  34. }
  35. }
  36. while (!st.empty()) {
  37. top = st.top();
  38. st.pop();
  39. area = heights[top] * (st.empty() ? n : st.top() - i - 1);
  40. mxarea = max(mxarea, area);
  41. }
  42. return mxarea;
  43. }
  44. int solve() {
  45. int n;
  46. cin>>n;
  47. vector<ll> v(n);
  48. for(int i=0;i<n;i++){
  49. cin>>v[i];
  50. }
  51. cout<<largestRectangleArea(v)<<endl;
  52.  
  53.  
  54. return 0;
  55. }
  56.  
  57. int main() {
  58. fastio();
  59. File();
  60. int32_t t=1;
  61. cin>>t;
  62. for(auto i=t;i--;){
  63. solve();
  64. }
  65. }
  66.  
Success #stdin #stdout 0s 5284KB
stdin
2
7
2 1 4 5 1 3 3
4
1000 1000 1000 1000
stdout
8
4000