152. 城市游戏 AcWing

原题链接

思路是来自y总的视频...我的第一反应是dfs....

求最大矩形面积我们可以联想到那道经典的单调栈例题,因为这道题的n、m都不是很大,所以我们可以用O(n^2)的算法。枚举每一行,将每一行以及上面的矩形都看成直方图,即把每列F的个数当作那道题的阴影部分的高度,然后我们在每一行求它的左右最远距离,那么这道题就和那道经典题没有任何区别

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 1010;
 4 char mp[N][N];
 5 int cnt[N][N],maxn = 1<<31;
 6 int main()
 7 {
 8     int n,m;
 9     scanf("%d%d",&n,&m);
10     for(int i=1;i<=n;i++){
11         for(int j=1;j<=m;j++){
12             cin>>mp[i][j];
13             if(mp[i][j]=='F') cnt[i][j] = 1;
14             cnt[i][j]+=cnt[i-1][j];
15             if(mp[i][j]=='R') cnt[i][j] = 0;
16         }
17     }
18     for(int i=1;i<=n;i++){//单调递增栈
19         stack<int> stk;
20         int L[N],R[N];
21         for(int j=1;j<=m;j++){
22             while(!stk.empty()&&cnt[i][stk.top()]>=cnt[i][j]) stk.pop();
23             if(stk.empty()) L[j] = 1;
24             else L[j] = stk.top()+1;
25             stk.push(j);
26         }
27         while(!stk.empty()) stk.pop();
28         for(int j=m;j>0;j--){
29             while(!stk.empty()&&cnt[i][stk.top()]>=cnt[i][j]) stk.pop();
30             if(stk.empty()) R[j] = m;
31             else R[j] = stk.top()-1;
32             stk.push(j);
33         }
34         for(int j=1;j<=m;j++)
35             maxn = max((R[j]-L[j]+1)*cnt[i][j],maxn);
36     }
37     printf("%d\n",maxn*3);
38     return 0;
39 }

 

一道题换个样子我就不会了orz

不要看到矩阵就dfs!!!

posted @ 2020-12-29 13:52  acmloser  阅读(102)  评论(0编辑  收藏  举报