思路:
预处理好前缀和,枚举上边界和下边界,将二维变成一维,用单调队列找满足题意的最小前缀
复杂度,O(r*r*c)
代码:
#pragma GCC optimize(2)#pragma GCC optimize(3)#pragma GCC optimize(4)#includeusing namespace std;#define fi first#define se second#define pi acos(-1.0)#define LL long long//#define mp make_pair#define pb push_back#define ls rt<<1, l, m#define rs rt<<1|1, m+1, r#define ULL unsigned LL#define pll pair #define pii pair #define piii pair #define mem(a, b) memset(a, b, sizeof(a))#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);//headconst int N = 555;const LL INF = 0x7f7f7f7f7f7f7f7f;int a[N][N];LL sum[N][N], tot[N];int sm[N][N], cnt[N];deque q;int main() { int n, m, x, y, z; scanf("%d %d %d %d %d", &n, &m, &x, &y, &z); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]); } for (int j = 1; j <= m; j++) { for (int i = 1; i <= n; i++) { sum[i][j] = sum[i-1][j] + a[i][j]; sm[i][j] = sm[i-1][j] + (a[i][j] == 0); } } LL ans = -INF; for (int l = 1; l <= n; l++) { for (int r = l; r <= n && r <= l+x-1; r++) { q.clear(); tot[0] = 0; cnt[0] = 0; q.push_back(0); for (int i = 1; i <= m; i++) { tot[i] = tot[i-1] + sum[r][i] - sum[l-1][i]; cnt[i] = cnt[i-1] + sm[r][i] - sm[l-1][i]; while(!q.empty() && tot[q.back()] >= tot[i]) q.pop_back(); q.push_back(i); while(!q.empty() && q.front() < i - y) q.pop_front(); while(!q.empty() && cnt[i] - cnt[q.front()] > z) q.pop_front(); if(!q.empty()) ans = max(ans, tot[i] - tot[q.front()]); } } } printf("%lld\n", ans); return 0;}