欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

最大全 1 矩阵 - 单调栈

程序员文章站 2022-07-12 09:22:06
...

Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is the largest? By largest we mean that the submatrix has the most elements.

Input

The input contains multiple test cases. Each test case begins with m and n (1 ≤ m, n ≤ 2000) on line. Then come the elements of a (0,1)-matrix in row-major order on m lines each with n numbers. The input ends once EOF is met.

Output

For each test case, output one line containing the number of elements of the largest submatrix of all 1’s. If the given matrix is of all 0’s, output 0.

Sample Input
2 2
0 0
0 0
4 4
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0
Sample Output
0
4

题意:给你一个 n*m 的矩阵,找出内部全 1 矩阵的最大面积是多少?
思路分析:这是一个经典的问题,我们首先要进行一个预处理操作,即在每行的每一个位置上连续的最大的 1 的矩阵的高度是多少,再对每一行进行一个单调栈处理即可
代码示例:
typedef pair<int, int>pa;

int n, m;
int a[2005][2005];
pa f[2005];
stack<pa>s;

int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    
    while(~scanf("%d%d", &n, &m)){
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                scanf("%d", &a[i][j]);
                if (a[i][j]) a[i][j] += a[i-1][j];
            }
        }
        
        int ans = 0;
        pa v;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                if (s.empty() && !a[i][j]) continue;
                
                if (s.empty() || s.top().second <= a[i][j]) s.push(make_pair(j, a[i][j])); 
                else {
                    while(!s.empty() && s.top().second > a[i][j]){
                        v = s.top();
                        s.pop();
                        int area = (j-v.first)*v.second;
                        if (area > ans) ans = area;    
                    }
                    s.push(make_pair(v.first, a[i][j]));
                }
                
            } 
        }
        printf("%d\n", ans);
    }
    return 0;
}

 

Feeling hungry, a cute hamster decides to order some take-away food (like fried chicken for only 303030 Yuan).

However, his owner CXY thinks that take-away food is unhealthy and expensive. So she demands her hamster to fulfill a mission before ordering the take-away food. Then she brings the hamster to a wall.

The wall is covered by square ceramic tiles, which can be regarded as a n∗mn * mnm grid. CXY wants her hamster to calculate the number of rectangles composed of these tiles.

For example, the following 3∗33 * 333 wall contains 363636 rectangles:

最大全 1 矩阵 - 单调栈

Such problem is quite easy for little hamster to solve, and he quickly manages to get the answer.

Seeing this, the evil girl CXY picks up a brush and paint some tiles into black, claiming that only those rectangles which don't contain any black tiles are valid and the poor hamster should only calculate the number of the valid rectangles. Now the hamster feels the problem is too difficult for him to solve, so he decides to turn to your help. Please help this little hamster solve the problem so that he can enjoy his favorite fried chicken.

Input

There are multiple test cases in the input data.

The first line contains a integer TTT : number of test cases. T≤5T \le 5T5.

For each test case, the first line contains 333 integers n,m,kn , m , kn,m,k , denoting that the wall is a n×mn \times mn×m grid, and the number of the black tiles is kkk.

For the next kkk lines, each line contains 222 integers: x yx\ yx y ,denoting a black tile is on the xxx-th row and yyy-th column. It's guaranteed that all the positions of the black tiles are distinct.

For all the test cases,

1≤n≤105,1≤m≤1001 \le n \le 10^5,1\le m \le 1001n105,1m100,

0≤k≤105,1≤x≤n,1≤y≤m0 \le k \le 10^5 , 1 \le x \le n, 1 \le y \le m0k105,1xn,1ym.

It's guaranteed that at most 222 test cases satisfy that n≥20000n \ge 20000n20000.

Output

For each test case, print "Case #xxx: ansansans" (without quotes) in a single line, where xxx is the test case number and ansansans is the answer for this test case.

Hint

The second test case looks as follows:

最大全 1 矩阵 - 单调栈

样例输入

2
3 3 0
3 3 1
2 2

样例输出

Case #1: 36
Case #2: 20

题意 : 给你一个 n*m 的矩阵,同时会挖空一些点,问此时的图中子矩阵的数量有多少?
思路分析 : 和上面的题目很类似,依旧我们先预处理一下,在每个位置,行的高度最多可以是多少,然后去计算以每一个位置为右下角矩阵的个数
    要怎么去计算呢?
    如计算以(x,y)为结尾的子矩阵的个数显然是 sigma(f(i)) (1 <= i <= y) , f(i) 是 up[i]-( (i+1, y) 中行高的最小值), 显然这里用一个单调栈去维护即可
代码示例 :
#define ll long long
const ll maxn = 1e5+5;
const ll mod = 1e9+7;
const double eps = 1e-9;
const double pi = acos(-1.0);
const ll inf = 0x3f3f3f3f;
typedef pair<ll, ll> P;
#define fi first
#define se second

ll n, m, kk;
ll up[maxn][105];
bool mp[maxn][105];

void init() { 
    memset(up, 0, sizeof(up));
    for(ll j = 1; j <= m; j++){
        for(ll i = 1; i <= n; i++){
            if (!mp[i][j]) up[i][j] = up[i-1][j]+1;
        }
    } 
}

stack<P>s;
ll sum[105];
ll cas = 1;
void solve() {
    ll ans = 0;
    
    for(ll i = 1; i <= n; i++){
        while(!s.empty()) s.pop();
        s.push(P(0, 0)); // pos , height ,先加一个进去,方便计算
        for(ll j = 1; j <= m; j++){
            while(s.top().se > up[i][j]) s.pop();
            sum[j] = sum[s.top().fi] + (j-s.top().fi)*up[i][j]; // O(1)计算
            ans += sum[j];            
            s.push(P(j, up[i][j]));
        }
    }    
    printf("Case #%lld: %lld\n",cas++, ans); 
}

int main() {
    ll t;
    ll x, y;
     
    cin >> t;
    while(t--){
        scanf("%lld%lld%lld", &n, &m, &kk);
        memset(mp, false, sizeof(mp));
        while(kk--){
            scanf("%lld%lld", &x, &y);
            mp[x][y] = true;
        }
        init();
        solve();
    }
    return 0;
}