HDU -- 1506 -- matrix summation <stack>

Source: Internet
Author: User

Cao oh, let me vent a mistake. I found my mistake for more than half an hour.

Really Sb...

A game time...

Touch me first

This is the simple version of the previous question =-= It is very easy to master this question.

I am suffering from this mistake. I don't want to talk about it any more.

This stack is hard to think of, but it is easy to understand, and leave a message to me =-= lack of human interaction is so lonely...

 1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4  5 typedef long long LL; 6 const int size = 100010; 7 int Left[size]; 8 int Right[size]; 9 LL h[size];10 11 int main()12 {13     int i , n;14     LL ans , area;15     while( cin >> n && n )16     {17         ans = 0;18         h[0] = h[n+1] = -1;19         for( i = 1 ; i<=n ; i++ )20         {21             cin >> h[i];22             Left[i] = Right[i] = i;23         }24         for( i = 2 ; i<=n ; i++ )25         {26             while( h[ Left[i]-1 ] >= h[i] )27             {28                 Left[i] = Left[ Left[i]-1 ];29             }30         }31         for( i = n-1 ; i>=1 ; i-- )32         {33             while( h[ Right[i]+1 ] >= h[i] )34             {35                 Right[i] = Right[ Right[i]+1 ];36             }37         }38         for( i = 1 ; i<=n ; i++ )39         {40             area = (LL) h[i] * ( Right[i] - Left[i] + 1 );41             ans = max( ans , area );42         }    43         cout << ans << endl;44     }45     return 0;46 }
View code

 

 1 #include <iostream> 2 #include <stack> 3 #include <algorithm> 4 using namespace std; 5  6 typedef long long LL; 7 const int size = 100010; 8 LL h[size]; 9 struct data10 {11     int w;12     LL h;13     data( int x , LL y )14     {15         w = x;16         h = y;17     }18 };19 stack<data>s;20 21 int main()22 {23     cin.sync_with_stdio(false);24     int n , totalW;25     LL area , ans;26     while( cin >> n && n )27     {28         for( int i = 0 ; i<n ; i++ )29         {30             cin >> h[i];31         }32         ans = h[n] = 0;33         while( !s.empty() )34             s.pop();35         for( int i = 0 ; i<=n ; i++ )36         {37             if( s.empty() || h[i] > s.top().h )38             {39                 s.push( data( 1,h[i] ) );40             }41             else if( h[i] == s.top().h )42             {43                 s.top().w ++;44             }45             else46             {47                 totalW = 0;48                 while( !s.empty() && h[i] < s.top().h )49                 {50                     totalW += s.top().w;51                     area = (LL)totalW * s.top().h;52                     ans = max( ans , area );53                     s.pop();54                 }55                 s.push( data( totalW+1,h[i] ) );56             }57         }58         cout << ans << endl;59     }60     return 0;61 }
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.