C. Maximum Subrectangle(前缀和 + 思维)

题目链接
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
                                                               ∑i=x1x2∑j=y1y2ci,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 ci,j≤x, output the largest value of (x2−x1+1)×(y2−y1+1) among all such quadruplets, otherwise output 0.

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):

                                                                                                       在这里插入图片描述

题解:

公式推导:
在这里插入图片描述
思路:通过公式可知,矩阵C和矩阵A、B并没有什么直接的联系,所有我们分开算即可,又因为题目要求的是方格数在小于等于x的情况下最大,我们我们尽可能在长度一定的情况下让里面的数最小,这样方格数才能最大,最后枚举A、B矩阵的长度即可

#include<bits/stdc++.h>
using namespace std;
const int N = 2005;
typedef long long ll;
ll n,m,k,res,a[N],b[N],mina[N],minb[N];
int main(){
	scanf("%lld%lld",&n,&m);
	for(int i = 1;i <= n;i++){
		scanf("%lld",a + i);
		a[i] += a[i - 1];
	} 
	for(int i = 1;i <= m;i++){
		scanf("%lld",b + i);
		b[i] += b[i - 1];
	}
	scanf("%lld",&k);
	memset(mina,0x3f,sizeof mina);
	memset(minb,0x3f,sizeof minb);
	for(int i = 1;i <= n;i++)
		for(int j = i;j <= n;j++)
			mina[i] = min(mina[i],a[j] - a[j - i]);
	for(int i = 1;i <= m;i++)
		for(int j = i;j <= m;j++)
			minb[i] = min(minb[i],b[j] - b[j - i]);
	res = 0;
	for(int i = 1;i <= n;i++)
		for(int j = 1;j <= m;j++)
			if(mina[i] * minb[j] <= k) res = max(res,i * 1ll * j);
	printf("%lld\n",res);
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值