Codeforces Round #513 by Barcelona Bootcamp (rated, Div. 1 + Div. 2) C

C. Maximum Subrectangle
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given two arrays a
and b of positive integers, with length n and m

respectively.

Let c
be an n×m matrix, where ci,j=ai⋅bj

.

You need to find a subrectangle of the matrix c
such that the sum of its elements is at most x

, and its area (the total number of elements) is the largest possible.

Formally, you need to find the largest number s
such that it is possible to choose integers x1,x2,y1,y2 subject to 1≤x1≤x2≤n, 1≤y1≤y2≤m, (x2−x1+1)×(y2−y1+1)=s, and x2∑i=x1y2∑j=y1ci,j≤x.

Input

The first line contains two integers n
and m (1≤n,m≤2000

).

The second line contains n
integers a1,a2,…,an (1≤ai≤2000

).

The third line contains m
integers b1,b2,…,bm (1≤bi≤2000

).

The fourth line contains a single integer x
(1≤x≤2⋅109

).
Output

If it is possible to choose four integers x1,x2,y1,y2
such that 1≤x1≤x2≤n, 1≤y1≤y2≤m, and ∑x2i=x1∑y2j=y1ci,j≤x, output the largest value of (x2−x1+1)×(y2−y1+1) among all such quadruplets, otherwise output 0

.
Examples
Input
Copy

3 3
1 2 3
1 2 3
9

Output
Copy

4

Input
Copy

5 1
5 4 2 4 5
2
5

Output
Copy

1


给定一个矩阵,求一个子矩阵的sum<=x的面积的最大值;

n,m∈[ 0,2000 ];


思路:
题目中给了 a,b 两个数组;
其中 c [ i ] [ j ]=a [ i ] * b [ j ];
我之前的思路:
明显可以预处理出 c[ i ] [ j ];
接着可以预处理出 二维前缀和;
如果枚举左上点和右下点,复杂度 O(N^4);
T到飞起23333


很显然;
子矩阵的和就是对应区间的和的乘积;
区间和可以前缀和预处理;
我们要求某个长度和某个宽度的子矩阵,那么我们只需要存储该长度或宽度对应的区间前缀和的min值;
貌似有点绕
那么O(N^2)就可以了;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 1000005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 10000007;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-10
const int N = 1505;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

ll cal(ll x) {
	ll ans = 0;
	while (x) {
		ans += (x % 10);
		x /= 10;
	}
	return ans;
}

ll a[4002], b[4002];
ll suma[4002], sumb[4002];
int n, m;
ll x;
ll SA[4003], SC[4003];

int main()
{
	//ios::sync_with_stdio(false);
	rdint(n); rdint(m);
	for (int i = 1; i <= n; i++) {
		rdllt(a[i]); suma[i] = suma[i - 1] + a[i];
	}
	for (int i = 1; i <= m; i++) {
		rdllt(b[i]); sumb[i] = sumb[i - 1] + b[i];
	}
	rdllt(x);
	int ans = 0;
	for (int i = 1; i <= max(n, m); i++)SA[i] = SC[i] = x + 1;
	for (int i = 1; i <= n; i++) {
		for (int j = i; j <= n; j++) {
			SA[j - i + 1] = min(SA[j - i + 1], suma[j] - suma[i - 1]);
		}
	}
	for (int i = 1; i <= m; i++) {
		for (int j = i; j <= m; j++) {
			SC[j - i + 1] = min(SC[j - i + 1], sumb[j] - sumb[i - 1]);
		}
	}

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (i*j > ans&&SA[i] * SC[j] <= x)ans = i * j;
		}
	}
	cout << ans << endl;
}

猜你喜欢

转载自blog.csdn.net/qq_40273481/article/details/82940067