Maximum Subrectangle(矩阵,前缀和)

You are given two arrays aa and bb of positive integers, with length nn and 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):
在这里插入图片描述
这个题,作为渣渣的我感觉挺难得。大体意思就是,两个矩阵相乘,出来一个nm矩阵,在这个矩阵中找一个子矩阵使得这个子矩阵的和小于等于x但是矩阵的面积是最大的。
我们要知道,子矩阵的和=(a[1]+a[2]+a[3]+…+a[j])
(b[1]+b[2]+…+b[i])这就是i*j矩阵的最小值。那我们应该做的就是找到前缀和最小的。在代码里的mina[i]=j;代表长度为i的数组最小值是j。
代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

const int maxx=2e3+10;
int n,m;
ll a[maxx];
ll b[maxx];
ll mina[maxx];
ll minb[maxx];
ll x;

int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		a[0] = b[0]=0;
		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];
		}
		memset(mina,inf,sizeof(mina));//先将那两个数组赋予最大值
		memset(minb,inf,sizeof(minb));
		for(int i=1;i<=n;i++)
		{
			for(int j=i;j<=n;j++)
			{
				mina[j-i+1]=min(mina[j-i+1],a[j]-a[i-1]);//动态规划,代表长度为i的最小值。
			}
		}
		for(int i=1;i<=m;i++)
		{
			for(int j=i;j<=m;j++)
			{
				minb[j-i+1]=min(minb[j-i+1],b[j]-b[i-1]);
			}
		}
		scanf("%I64d",&x);
		int ans=0;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				if(mina[i]*minb[j]<=x)//找到小于x的面积最大的。
				ans=max(ans,i*j);
			}
		}
		cout<<ans<<endl;
	}
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值