日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

2019牛客第八场A All-one Matrices(单调栈)

發布時間:2023/12/15 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2019牛客第八场A All-one Matrices(单调栈) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈接:https://ac.nowcoder.com/acm/contest/888/A

題目描述
Gromah and LZR entered the great tomb, the first thing they see is a matrix of size n×mn\times mn×m, and the elements in the matrix are all 00_{}0? or 11_{}1?.

LZR finds a note board saying “An all-one matrix is defined as the matrix whose elements are all 11_{}1?, you should determine the number of all-one submatrices of the given matrix that are not completely included by any other all-one submatrices”.

Meanwhile, Gromah also finds a password lock, obviously the password should be the number mentioned in the note board!

Please help them determine the password and enter the next level.

輸入描述:
The first line contains two positive integers n,mn,m_{}n,m?, denoting the size of given matrix.

Following nn_{}n? lines each contains a string with length mm_{}m?, whose elements are all 00_{}0? or 11_{}1?, denoting the given matrix.

1≤n,m≤30001\le n,m \le 30001≤n,m≤3000

輸出描述:
Print a non-negative integer, denoting the answer.
輸入
3 4
0111
1110
0101
輸出
5
說明
The 5 matrices are (1,2)?(1,4),??(1,2)?(2,3),??(1,2)?(3,2),??(2,1)?(2,3),??(3,4)?(3,4)(1,2)-(1,4), ; (1,2)-(2,3), ; (1,2)-(3,2), ; (2,1)-(2,3), ; (3,4)-(3,4)_{}(1,2)?(1,4),(1,2)?(2,3),(1,2)?(3,2),(2,1)?(2,3),(3,4)?(3,4)?.
賽后補題,賽場上一點思路沒有。。
poj上有一個最大全一矩陣的題目,也是單調棧的題目。但是沒想起來怎么用單調棧解決這個問題。對于每一個num[i][j]我們記錄它向上的連續一的個數,然后枚舉每一行,從前向后枚舉列。單調找維護兩個東西,就是向上的最長長度和矩陣的最左位置pos。那么我們把最上最左可到達的距離記錄下來了,不能向右擴,那么只看能不能向下擴就行了。如果不能的話,就到頭了。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=3e3+100; int a[maxx][maxx]; int num[maxx][maxx]; stack< pair<int,int> > s; int n,m;int main() {scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){for(int j=1;j<=m;j++)scanf("%1d",&a[i][j]);}for(int i=1;i<=n;i++){for(int j=1;j<=m;j++)num[i][j]=a[i][j]?num[i-1][j]+a[i][j]:0;//記錄每一列的連續1的前綴和 }int ans=0;for(int i=1;i<=n;i++){int tmp=-1;while(!s.empty()) s.pop();for(int j=1;j<=m+1;j++){int pos=j;while(!s.empty()&&s.top().first>num[i][j]){if(s.top().second<=tmp) ans++;//這個矩陣最左邊小于當前不可擴的最右邊,就說明這個矩陣不可擴 pos=s.top().second;//更新pos就是下個矩陣的最左邊。 s.pop();}if(!num[i+1][j]) tmp=j;//下面是0就不可下擴,那樣就把最右邊的記錄下來 if(num[i][j]&&(s.empty()||s.top().first<num[i][j])) s.push(make_pair(num[i][j],pos));//入棧的是矩陣的最左邊(i+num-1,j)->(i,j)就是一個矩陣 }}printf("%d\n",ans); }

努力加油a啊,(o)/~

總結

以上是生活随笔為你收集整理的2019牛客第八场A All-one Matrices(单调栈)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。