CF1060C Maximum Subrectangle

题意翻译

现在给出一个长度为N的a数列,一个长度为M的b数列. 现在需要构造出一个矩阵c,其中ci,j​=ai​×bj​.再给出一个x,请在矩阵中找出一个最大的矩形,使得这个矩形中的所有值的和小于等于x.

题目描述

You are given two arrays aa and bb 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 n1≤x1​≤x2​≤n , m1≤y1​≤y2​≤m , (x2​−x1​+1)×(y2​−y1​+1)=s , and $\sum_{i=x_1}^{x2}{\sum_{j=y_1}^{y2}{c{i,j}}} \leq x.$

输入输出格式

输入格式:

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

输出格式:

If it is possible to choose four integersx1​,x2​,y1​,y2​ such that n1≤x1​≤x2​≤n ,  m1≤y1​≤y2​≤m , and x∑i=x1​x2​​∑j=y1​y2​​ci,j​≤x , output the largest value of (x2​−x1​+1)×(y2​−y1​+1) among all such quadruplets, otherwise output 0 .

输入输出样例

输入样例#1: 复制

3 3
1 2 3
1 2 3
9
输出样例#1: 复制

4
输入样例#2: 复制

5 1
5 4 2 4 5
2
5
输出样例#2: 复制

1

说明

Matrix from the first sample and the chosen subrectangle (of blue color):

Matrix from the second sample and the chosen subrectangle (of blue color):


Solution

没想到是道水题QAQ

可以发现,一个子矩阵的值实际上就是这个子矩阵包括的$a$和$b$数组的乘积,根据乘法分配律可得。

所以可以预处理出长度一定时最小的$a、b$区间,然后双指针扫描即可。

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#define LL long long
using namespace std; int n, m;
LL a[], b[], x;
LL suma[], sumb[], ans, maa[], mab[]; int main() {
memset(maa, 0x3f3f3f3f, sizeof(maa));
memset(mab, 0x3f3f3f3f, sizeof(mab));
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i ++) {
scanf("%lld", &a[i]);
suma[i] = suma[i - ] + a[i];
for(int j = ; j <= i; j ++)
maa[j] = min(maa[j], suma[i] - suma[i - j]);
}
for(int i = ; i <= m; i ++) {
scanf("%lld", &b[i]);
sumb[i] = sumb[i - ] + b[i];
for(int j = ; j <= i; j ++)
mab[j] = min(mab[j], sumb[i] - sumb[i - j]);
}
scanf("%lld", &x);
LL j = ;
for(LL i = m; i >= ; i --) {
while(j < n && maa[j + ] * mab[i] <= x)
j ++;
ans = max(ans, j * i);
}
printf("%lld", ans);
return ;
}

CF1060C Maximum Subrectangle【乘法分配律】【最大子矩阵】的更多相关文章

  1. cf1060C. Maximum Subrectangle(思维 枚举)

    题意 题目链接 Sol 好好读题 => 送分题 不好好读题 => 送命题 开始想了\(30\)min数据结构发现根本不会做,重新读了一遍题发现是个傻逼题... \(C_{i, j} = a ...

  2. CF1060C Maximum Subrectangle

    思路: 不难发现,对矩阵中的数字求和实际上是先分别对a,b两个数列中对应子段的元素求和再相乘.题目是要求在和不超过给定值的情况下使选出的矩阵面积最大.我们反其道而行之,考虑在子段长度一定的情况下,和最 ...

  3. Codeforces 1060C Maximum Subrectangle(子矩阵+预处理)

    题意:给出数组a,b,组成矩阵c,其中$c_{ij}=a_i*b_j$,找出最的大子矩阵,使得矩阵元素和<=x,求这个矩阵的size n,m<=2000 思路:对于子矩阵(l1...r1) ...

  4. 矩阵乘法分配律+bitset优化——hdu4920

    因为是模3,所以把原矩阵拆成两个01矩阵,然后按分配律拆开分别进行矩阵乘法,行列用bitset来存进行优化即可 注意 int bitset<int>::count() 函数可以统计bits ...

  5. C. Maximum Subrectangle

    链接 [http://codeforces.com/contest/1060/problem/C] 题意 给你两个数列,可以构成一个矩阵C,ci,j=ai⋅bj 1≤x1≤x2≤n , 1≤y1≤y2 ...

  6. Codeforces Round #513 by Barcelona Bootcamp C. Maximum Subrectangle(双指针+思维)

    https://codeforces.com/contest/1060/problem/C 题意 给两个数组,a数组有n个元素,b数组有m个元素,两个数组元素互相相乘形成n*m的矩阵,找一个子矩阵,元 ...

  7. codeforces_C. Maximum Subrectangle

    http://codeforces.com/contest/1060/problem/C 题意: a.b数组长度分别为n.m.矩阵C,Cij=ai*bj.在C中找到一个子矩阵,该子矩阵所有元素和不大于 ...

  8. Codeforces Round #513

    A. Phone Numbers 题意:给一些数字,每个电话号码以8开头,11位,求最多组成多少个号码,重复累加. #include <bits/stdc++.h> using names ...

  9. Codevs No.3147 矩阵乘法2

    2016-06-01 17:33:30 题目链接: 矩阵乘法2 (Codevs No.3147) 题目大意: 给定两个大小相同的正方形矩阵A,B.多次询问,每次求乘后矩阵的一个子矩阵所有元素的和. 解 ...

随机推荐

  1. 关于limit_req和limit_conn的区别

    1,首先,limit_req和limit_conn两个模块都是为了来限流的,但是两者不在一个层面,为了搞清楚这个,必须先要弄清楚request和connection的区别,因为在很多情况下,我们把他们 ...

  2. QEMU漏洞挖掘

    转载:https://www.tuicool.com/articles/MzqYbia qemu是一个开源的模拟处理器硬件设备的全虚拟化仿真器和虚拟器. KVM(kernel virtual mach ...

  3. Ubuntu下安装arm-linux-gnueabi-xxx编译器【转】

    转自:http://blog.csdn.net/real_myth/article/details/51481639 from: http://www.linuxdiyf.com/linux/1948 ...

  4. Ubuntu server 搭建Git server【转】

    转自:http://www.cnblogs.com/candle806/p/4064610.html Ubuntu server 搭建Git server,git相比svn,最主要就是分布式了,每个客 ...

  5. mysql命令补全工具

    需要在linux中下载mysql插件. 安装mysql插件 yum -y install epel-release python-pip python-devel pip install mycli ...

  6. Java之反转排序

    顾名思义,反转排序就是以相反的顺序把原来的数组内容重新进行排序.反转排序算法在我们的程序开发中也是经常用到的.而反转排序的基本思想也很简单,就是把数组最后一个元素与第一个元素进行交换,倒数第二个与第二 ...

  7. CF 577A 分解因数

    输入一个n 构成一个n*n的表 这个表里的数 第i行j列的值为i*j 问x在这个表里出现了几次 Sample test(s)input10 5output2input6 12output4input5 ...

  8. (使用通过混淆+自己第三方保留成功混淆)AndroidStudio 混淆打包

    原文:https://blog.csdn.net/mazhidong/article/details/64820838 AndroidStudio中的项目可以用compile的形式引入github上的 ...

  9. C语言:10个整数排序(别忘了负数)

    题目内容: 10个整数排序(别忘了负数) 例如 input 1 0 2 0 3 4 1 9 8 7 output 0 0 1 1 2 3 4 7 8 9 编码: void sort(int *a); ...

  10. 易普优APS与国外知名高级计划排程系统对比

    众所周知软件执行效率受制于硬件性能,市面上的APS产品多为单机版本,企业要应用好APS,保证紧急插单.计划下发全程无忧,用户电脑硬件性能是不容忽视的一大瓶颈.APS的直接用户是车间管理人员.计划员,而 ...