HDU 1505 City game-dp-(maximum sub-matrix model)

Source: Internet
Author: User

Test instructions: Finding the largest sub-matrix

Analysis: Directly using the formula of the most large matrix to do time-out. A change of mind, the problem with the previous question 1506 has a relationship, first of each layer to calculate the maximum height of each element can reach, and then with 15,061 samples. The DP staging data is used for height and area two locations. For the height of the double cycle, and then each layer for the bottom area to the double loop plus the outer layer is the triple loop, but because the DP is used to save the intermediate results, so this triple loop will not time out. DP[J] represents the maximum height that the current Layer J column can reach, State transfer: 1.a[i][j]== ' R ', dp[j]=0;2. Otherwise, if a[i-1][j]== ' F ', then dp[j]++ (this uses the DP, saving the front layer of data, so directly +1 can) If a[i-1][j]== ' R ', then dp[j]=1,. The algorithm for calculating area per layer is shown in 1506.

Code:

#include <iostream>using namespace Std;int t,m,n;int mx,sum;int L[1005],r[1005],dp[1005];char a[1005][1005];int Max (int I,int j) {return i>j?i:j;}  void Fir ()//handles the first layer {mx=0;for (int j=0;j<n;j++) if (a[0][j]== ' F ') dp[j]=1; else dp[j]=0;l[0]=0,r[n-1]=n-1;for (int i=1;i<n;i++) {int t=i;while (t>0&&dp[i]<=dp[t-1]) t=l[t-1];l [I]=t;} for (int i=n-2;i>=0;i--) {int t=i;while (t<n-1&&dp[i]<=dp[t+1]) t=r[t+1];r[i]=t;} for (int i=0;i<n;i++) {sum=dp[i]* (r[i]-l[i]+1); Mx=max (mx,sum);}} void DP () {fir (); for (int. i=1;i<m;i++) {for (int j=0;j<n;j++) {if (a[i][j]== ' R ') dp[j]=0;else{if (a[i-1][j]== ' F ') d    p[j]++; else dp[j]=1;}} l[0]=0,r[n-1]=n-1;for (int i=1;i<n;i++) {int t=i;while (t>0&&dp[i]<=dp[t-1]) t=l[t-1];l[i]=t;} for (int i=n-2;i>=0;i--) {int t=i;while (t<n-1&&dp[i]<=dp[t+1]) t=r[t+1];r[i]=t;}    for (int i=0;i<n;i++) {sum=dp[i]* (r[i]-l[i]+1); Mx=max (mx,sum); }}}int Main () {cin>>t;while (t--) {cin>>m>>n;for (int i=0; i<m;i++) for (int j=0;j<n;j++) cin>>a[i][j];DP(); cout<<mx*3<<endl;}} 


HDU 1505 City game-dp-(maximum sub-matrix model)

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.