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. SolrJ查询条件组合查询实现——(十六)

    带查询条件的实现原理: 查询按钮被包在一个大表单,表单还有三个隐藏域,一个商品筛选,一个 价格,一个排序,每次点击查询时候清空三个隐藏域,就带着一个大条件去查询;点击下面的筛选条件时,给隐藏域的筛选条 ...

  2. 配置replica set的常见问题

    总有人问起配置ReplicaSet不成功,总结了一下基本上的可能性就几种,检查步骤如下: 假设三台机器的IP分别是 A: 192.168.1.2 a.test.com B:192.168.1.3 b. ...

  3. no libsigar-amd64-linux.so in java.library.path 解决方法

    关于sigar的介绍可以参考这边博文 :https://www.cnblogs.com/luoruiyuan/p/5603771.html 在Linux上运行java程序时出现 no libsigar ...

  4. 使用dork脚本来查询Google

    使用dork脚本来查询Google 了解Google Hacking数据库的第一步是了解所有典型的Google运算,就像机器级编程工程师必须了解计算机操作代码一样. 这些Google运算是Google ...

  5. openstack发展历程及其架构简介

    1.0 Openstack介绍 OpenStack既是一个社区,也是一个项目和一个开源软件,它提供了一个部署云的操作平台或工具集.其宗旨在于,帮助组织运行为虚拟计算或存储服务的云,为公有云.私有云,也 ...

  6. 运用java反射

    Class类 要正确使用Java反射机制就得使用java.lang.Class这个类.它是Java反射机制的起源.当一个类被加载以后,Java虚拟机就会自动产生一个Class对象.通过这个Class对 ...

  7. spring boot 中使用redis session

    spring boot 默认的httpsession是存在内存中.这种默认方式有几个缺点:1.当分布式部署时,存在session不一致的问题:2.当服务重启时session就会丢失,这时候用户就需要重 ...

  8. OR 连接查询注意

    用or 查询时, 取得是 每个or中条件的 查询的结果集union. select * from categorysecond t where ISNULL(null); ort.csid in (' ...

  9. 深度学习国外课程资料(Deep Learning for Self-Driving Cars)+(Deep Reinforcement Learning and Control )

    MIT(Deep Learning for Self-Driving Cars) CMU(Deep Reinforcement Learning and Control ) 参考网址: 1 Deep ...

  10. CVE-2012-1876Microsoft Internet Explorer Col元素远程代码执行漏洞分析

    Microsoft Internet Explorer是微软Windows操作系统中默认捆绑的WEB浏览器.         Microsoft Internet Explorer 6至9版本中存在漏 ...