hdu1501之動態規劃

來源:互聯網
上載者:User

題意簡述:輸入n,m代表n行m列,輸入的只有R,F兩種字元,R代表這塊地方不是閒置無法使用,F代表這個地方是閒置可以使用,求可以使用的最大面積,這個面積一定只能是矩形面積,最後輸出這個面積*3即價值。

這題是hdu1506的加強版,沒看hdu1506的建議先看下。

這題思路是把每一行作為底邊像hdu1506一樣,然後求的這一行的最大面積,最後求的所有行的最大面積中的最大一個就是所要求的結果.


#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cstdlib>#include<queue>#include<algorithm>#include<map>#include<iomanip>#define INF 99999999using namespace std;const int MAX=1001;int hight[2][MAX];//以第i行第j列作為一塊木板,每次求出以第i行為底的最大面積,最後求所有i行的最大面積中的最大一個即可. int Left[MAX],Right[MAX];//和hdu1056一樣求最左端和最右端. int main(){int n,m,t;char a;cin>>t;while(t--){    cin>>n>>m;int Max=0;for(int i=0;i<2;++i)hight[i][m+1]=hight[i][0]=-1;for(int i=1;i<=m;++i)hight[0][i]=0;for(int i=1;i<=n;++i){for(int j=1;j<=m;++j){cin>>a;if(a == 'F')hight[i%2][j]=hight[(i-1)%2][j]+1;//求第i行的第j列這塊木板的高度.else hight[i%2][j]=0;Left[j]=Right[j]=j; }//就是把hdu1505變一下,看成有i個hdu1506即可,每次求面積最大值. for(int j=1;j<=m;++j){//求連續的有這樣高度的木板的最左端. while(hight[i%2][Left[j]-1] >= hight[i%2][j])Left[j]=Left[Left[j]-1];}for(int j=m;j>=1;--j){//求連續的有這樣高度的木板的最右端. while(hight[i%2][Right[j]+1] >= hight[i%2][j])Right[j]=Right[Right[j]+1];}for(int j=1;j<=m;++j){if((Right[j]-Left[j]+1)*hight[i%2][j] > Max)Max=(Right[j]-Left[j]+1)*hight[i%2][j];}}cout<<Max*3<<endl;}return 0;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.