Maximum Subrectangle
You are given two arrays aa and bb of positive integers, with length nnand mm respectively.
Let cc be an n×mn×m matrix, where ci,j=ai⋅bjci,j=ai⋅bj.
You need to find a subrectangle of the matrix cc such that the sum of its elements is at most xx, and its area (the total number of elements) is the largest possible.
Formally, you need to find the largest number ss such that it is possible to choose integers x1,x2,y1,y2x1,x2,y1,y2 subject to 1≤x1≤x2≤n1≤x1≤x2≤n, 1≤y1≤y2≤m1≤y1≤y2≤m, (x2−x1+1)×(y2−y1+1)=s(x2−x1+1)×(y2−y1+1)=s, and
∑i=x1x2∑j=y1y2ci,j≤x.∑i=x1x2∑j=y1y2ci,j≤x.
Input
The first line contains two integers nn and mm (1≤n,m≤20001≤n,m≤2000).
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤20001≤ai≤2000).
The third line contains mm integers b1,b2,…,bmb1,b2,…,bm (1≤bi≤20001≤bi≤2000).
The fourth line contains a single integer xx (1≤x≤2⋅1091≤x≤2⋅109).
Output
If it is possible to choose four integers x1,x2,y1,y2x1,x2,y1,y2 such that 1≤x1≤x2≤n1≤x1≤x2≤n, 1≤y1≤y2≤m1≤y1≤y2≤m, and ∑x2i=x1∑y2j=y1ci,j≤x∑i=x1x2∑j=y1y2ci,j≤x, output the largest value of (x2−x1+1)×(y2−y1+1)(x2−x1+1)×(y2−y1+1) among all such quadruplets, otherwise output 00.
Examples
Input
3 3 1 2 3 1 2 3 9
Output
4
Input
5 1 5 4 2 4 5 2 5
Output
1
Note
Matrix from the first sample and the chosen subrectangle (of blue color):
Matrix from the second sample and the chosen subrectangle (of blue color):
题意:给你两个数组 a 和 b. 构造出一个矩阵c ,其中 c[i][j] = a[i] * b[j] . 求一个最大子矩阵的大小 使其和 <= x.
思路:仔细算一下会发现,这里的一个子矩阵的和 = (a[i] + a[i + 1] + ... + a[j]) * (b[i] + b[i + 1] + ... + b[j]). 故这里用两个数组分别维护a 和 b 前缀和。为了让 子矩阵和 <= x,又要使矩阵最大。 故需要再来两个数组存放 长度为 i 时的 最小的和。 最后暴力判断就可以了。复杂度 n^2。
AC代码:
#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define INT(t) int t; scanf("%d",&t)
#define LLI(t) LL t; scanf("%I64d",&t)
using namespace std;
const int maxn = 2100;
const int inf = 0x3f3f3f3f;
int a[maxn];
int b[maxn];
int mia[maxn];
int mib[maxn];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m)){
clr(mia,inf); clr(mib,inf);
for(int i = 1;i <= n;i ++){
scanf("%d",&a[i]);
a[i] += a[i - 1];
}
for(int i = 1;i <= m;i ++){
scanf("%d",&b[i]);
b[i] += b[i - 1];
}
LL x; scanf("%I64d",&x);
for(int i = 1;i <= n;i ++){
for(int j = i;j <= n;j ++){
mia[i] = min(mia[i],a[j] - a[j - i]);
}
}
for(int i = 1;i <= m;i ++){
for(int j = i;j <= m;j ++){
mib[i] = min(mib[i],b[j] - b[j - i]);
}
}
int ans = 0;
for(int i = 1;i <= n;i ++){
for(int j = 1;j <= m;j ++)
if((LL) mia[i] * (LL) mib[j] <= x)
ans = max(ans,i * j);
}
printf("%d\n",ans);
}
return 0;
}