2019牛客暑期多校训练营(第八场) All-one Matrices
关于矩形的单调栈处理,这是这类题目第三次见到了,每次都只是少许变化。
wa了很多发,模拟的时候还是应该尽量小心。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<map>
//#include<regex>
#include<cstdio>
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
int read()
{
char ch = getchar(); int x = 0, f = 1;
while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
const int N = 3e3 + 10;
char grp[N][N];
int hi[N];
int n, m,ans;
int main()
{
n = read(), m = read();
up(i,0,n)scanf("%s", grp[i]);
up(i, 0, n)
{
int x=-1;
int vec[N];
up(j, 0, m)
{
if (grp[i][j] == '1')hi[j] = hi[j] + 1;
else hi[j] = 0;
vec[j] = hi[j];
}
stack< pir > st;
vec[m] = 0;
upd(j, 0, m)
{
int temp = j;
if (st.empty() || vec[st.top().first] < vec[j])
{
st.push(make_pair(j, temp));
}
while (!st.empty() && vec[st.top().first] > vec[j])
{
if (st.top().second <= x)
{
ans++;
}
temp = st.top().second;
st.pop();
}
if (grp[i + 1][j] != '1')x = j;
if (hi[j]) {
if (st.empty())st.push(make_pair(j, temp));
if (vec[st.top().first] < vec[j])st.push(make_pair(j, temp));
}
}
}
cout << ans << endl;
}
橙橙橙