传送门:http://codeforces.com/contest/1060/problem/C
我们要找到一个子矩阵元素和小于x,元素个数最多,那么我们发现,只要从a数组中取出一段连续的元素,b中取出一段连续的元素,他们组成的矩阵一定是子矩阵,而且子矩阵元素的和为
sum = (a[i] + a[i+1] + .... + a[j]) * (b[ii] + b[ii+1] + ... + b[jj])
那么我们就可以先预处理出a,b数组的某段的和,然后数组记录a,b数组中长度为i的连续段的和的最小值,然后枚举a,b数组里段的长度,记录小于x的段,两段的长度乘积为元素个数,记录元素个数最多的那个即可。
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
inline int read(){
int f = 1, x = 0;char ch = getchar();
while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}
while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
return x * f;
}
const int maxn = 1e4 + 10;
int a[maxn],b[maxn],maxa[maxn],maxb[maxn];
int main(){
int n = read(),m = read();
for (int i = 1; i <= n; ++i) {
a[i] = read();
a[i] += a[i-1];
maxa[i] = INT_MAX;
}
for (int i = 1; i <= m; ++i) {
b[i] = read();
b[i] += b[i-1];
maxb[i] = INT_MAX;
}
int x = read(),maxx = 0;
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
maxa[j-i+1] = min(a[j]-a[i-1],maxa[j-i+1]);
}
}
for (int i = 1; i <= m; ++i) {
for (int j = i; j <= m; ++j) {
maxb[j-i+1] = min(b[j]-b[i-1],maxb[j-i+1]);
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if ((LL)maxa[i]*maxb[j] <= x){
maxx = max(i*j,maxx);
}
}
}
printf("%d\n",maxx);
return 0;
}