[Agc081F/At2699] Flip and Rectangles - 单调栈,结论

[Agc081F/At2699]
给出一个拥有 \(H\times W\) 个格子的棋盘,每个格子的颜色为黑色或白色。 Snuke 可以进行任意次下列操作:

选择棋盘中的一行或一列,将这一行或一列的颜色翻转(黑变成白,白变成黑) Snuke 想知道,在他进行操作后,棋盘中最大的全黑矩形最大能为多少。

考虑 \(2\times 2\) 方格,当且仅当偶数个黑时,可以做成全黑

大矩形能做成全黑,当且仅当所有 \(2\times 2\) 子格都是偶数个黑

然后就是一个很朴素的单调栈求最大矩形了

注意到答案最小为\(max(n,m)\),所以最后要处理一下

我大概是菜的连单调栈维护矩形都不会写了

#include <bits/stdc++.h>
using namespace std;

int h[2005],a[2005][2005],b[2005][2005],f[2005],p[2005],r,n,m,ans;
char c[2005][2005];

int main() {
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) {
        scanf("%s",c[i]+1);
        for(int j=1;j<=m;j++) {
            b[i][j] = c[i][j]=='#'?1:0;
        }
    }
    for(int i=1;i<n;i++) {
        for(int j=1;j<m;j++) {
            if((b[i][j]+b[i+1][j]+b[i][j+1]+b[i+1][j+1])%2==0) {
                a[i][j]=1;
            }
        }
    }
    for(int i=1;i<m;i++) h[i]=0;
    for(int i=1;i<n;i++) {
        for(int j=1;j<m;j++) {
            if(a[i][j]) h[j]++;
            else h[j]=0;
        }
        memset(f,0,sizeof f);
        memset(p,0,sizeof p);
        r=0;
        for(int j=1;j<=m;j++) {
            while(r && f[r] >= h[j]) {
                ans=max(ans, (j-p[r-1])*(f[r]+1));
                --r;
            }
            f[++r] = h[j];
            p[r] = j;
            //ans=max(ans, (j-p[1]+2)*(f[1]+1));
        }
        //while(r) if(i-h[p[r]]+1) ans=max(ans, (m-p[r]+1)*(f[r]+1)), r--;
        //if(r) cout<<(m-p[1]+1)<<" "<<(f[1]+1)<<endl;
    }
    cout<<max(ans,max(n,m))<<endl;
}

原文地址:https://www.cnblogs.com/mollnn/p/12267730.html

时间: 2024-10-09 22:11:32

[Agc081F/At2699] Flip and Rectangles - 单调栈,结论的相关文章

[arc081] F - Flip and Rectangles——思维题+单调栈

题目大意: 给定一个\(n\times m\)的01矩形,每次可以翻转一行或者翻转一列. 求翻转若干次之后的最大全1子矩形. 思路: 首先我们要知道一个结论:如果一个子矩形可以被翻转成为全1矩形,那么它内部的每一个\(2\times 2\)的子矩形的1的个数为偶数. 如果存在一个\(2\times 2\)的子矩形有奇数个1,那么无论怎么操作都还是奇数. 如果所有的\(2\times 2\)的子矩形都有偶数个1,我们可以先使这个矩形的第一行第一列都变为1,根据奇偶性不难发现整个矩阵此时必定全部都变

(单调栈)poj-2559 Largest Rectangle in a Histogram

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the

HDU4252 ACM fighting(单调栈)

Description After Mr. B arrived in Warsaw, he was shocked by the skyscrapers and took several photos. But now when he looks at these photos, he finds in surprise that he isn't able to point out even the number of buildings in it. So he decides to wor

hdu1506---Largest Rectangle in a Histogram(单调栈)

Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of

HDOJ 4252 A Famous City 单调栈

单调栈: 维护一个单调栈 A Famous City Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1671    Accepted Submission(s): 644 Problem Description After Mr. B arrived in Warsaw, he was shocked by the skyscrap

POJ 2559 Largest Rectangle in a Histogram(单调栈)

Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectang

POJ-2559 Largest Rectangle in a Histogram(单调栈)

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22171   Accepted: 7173 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi

浅谈单调队列、单调栈

       初谈这个话题,相信许多人会有一种似有所悟,但又不敢确定的感觉.没错,这正是因为其中"单调"一词的存在,所谓单调是什么,学过函数的people都知道单调函数或者函数的单调性,直白一点说单调就是一直增或一直减.例如:1,3,5,9就是一个单调增数列,数列中不存在后一个数比前一个数小的现象.那么同样,在这里谈到的话题也有类似特点.        先说一下单调队列吧!      单调队列,就是一个符合单调性质的队列,它同时具有单调的性质以及队列的性质.他在编程中使用频率不高,但却

POJ 2559 Largest Rectangle in a Histogram (单调栈或者dp)

Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15831   Accepted: 5121 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi