hdu--1506--矩陣求和<stack>

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   art   

草噢 先發泄一下 一個寫錯 TM地 找了我半小時多的錯誤 擦

真SB。。。

一把遊戲時間。。。

先放題目上來    touch  me  

這題 是上一題的簡單版=-=  上一題掌握了 這題 很簡單

被這個 找錯給折磨了 不想多講了

這個stack的寫法 難想到 但是容易看懂 不清楚的 留言 給我 =-= 缺人互動 好寂寞。。。

 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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.