CF 1060C. Maximum Subrectangle

给定两个正整数数组a和b,构建矩阵c,其中c[i][j]=a[i]*b[j]。寻找矩阵c的子矩形,使得元素之和小于等于x,同时最大化子矩形的面积。题目要求输出满足条件的最大面积。可以通过求a和b中连续子数组的最小值,然后暴力搜索解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C. Maximum Subrectangle
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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
∑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 ∑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
inputCopy
3 3
1 2 3
1 2 3
9
outputCopy
4
inputCopy
5 1
5 4 2 4 5
2
5
outputCopy
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,cij=ai*bj,然后给你一个数x,要你求c的子矩阵,使它们元素加起来的值小于等于x

思路:其实去模拟一遍会发现,其实就是求a,b数组中连续的数的最小值,最后暴力就可以了

#include<bits/stdc++.h>
#define LL long long
#define Max 2005
const LL mod=1e9+7;
const LL LL_MAX=9223372036;
using namespace std;
LL n,m;
LL a[Max],b[Max],suma[Max],sumb[Max];
LL ansa[Max],ansb[Max];
int main()
{
    for(int i=0;i<2005;i++)
        ansa[i]=ansb[i]=LL_MAX;
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++)//求前缀和
        scanf("%lld",&a[i]),suma[i]=suma[i-1]+a[i];
    for(int j=1;j<=m;j++)
        scanf("%lld",&b[j]),sumb[j]=sumb[j-1]+b[j];
    LL x;
    scanf("%lld",&x);
    for(int i=1;i<=n;i++){
        int t=0;
        for(int j=i;j<=n;j++)//算出i个数的最小值
            ansa[i]=min(ansa[i],suma[j]-suma[t]),t++;
    }
    for(int i=1;i<=m;i++){
        int t=0;
        for(int j=i;j<=m;j++)
            ansb[i]=min(ansb[i],sumb[j]-sumb[t]),t++;
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++)//暴力枚举
            if(ansa[i]*ansb[j]<=x)
                ans=max(ans,i*j);
    }
    printf("%d\n",ans);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值