CF1060C Maximum Subrectangle【乘法分配律】【最大子矩阵】
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=x1x2∑j=y1y2ci,j≤x , output the largest value of (x2−x1+1)×(y2−y1+1) among all such quadruplets, otherwise output 0 .
输入输出样例
说明
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【乘法分配律】【最大子矩阵】的更多相关文章
- cf1060C. Maximum Subrectangle(思维 枚举)
题意 题目链接 Sol 好好读题 => 送分题 不好好读题 => 送命题 开始想了\(30\)min数据结构发现根本不会做,重新读了一遍题发现是个傻逼题... \(C_{i, j} = a ...
- CF1060C Maximum Subrectangle
思路: 不难发现,对矩阵中的数字求和实际上是先分别对a,b两个数列中对应子段的元素求和再相乘.题目是要求在和不超过给定值的情况下使选出的矩阵面积最大.我们反其道而行之,考虑在子段长度一定的情况下,和最 ...
- Codeforces 1060C Maximum Subrectangle(子矩阵+预处理)
题意:给出数组a,b,组成矩阵c,其中$c_{ij}=a_i*b_j$,找出最的大子矩阵,使得矩阵元素和<=x,求这个矩阵的size n,m<=2000 思路:对于子矩阵(l1...r1) ...
- 矩阵乘法分配律+bitset优化——hdu4920
因为是模3,所以把原矩阵拆成两个01矩阵,然后按分配律拆开分别进行矩阵乘法,行列用bitset来存进行优化即可 注意 int bitset<int>::count() 函数可以统计bits ...
- C. Maximum Subrectangle
链接 [http://codeforces.com/contest/1060/problem/C] 题意 给你两个数列,可以构成一个矩阵C,ci,j=ai⋅bj 1≤x1≤x2≤n , 1≤y1≤y2 ...
- Codeforces Round #513 by Barcelona Bootcamp C. Maximum Subrectangle(双指针+思维)
https://codeforces.com/contest/1060/problem/C 题意 给两个数组,a数组有n个元素,b数组有m个元素,两个数组元素互相相乘形成n*m的矩阵,找一个子矩阵,元 ...
- codeforces_C. Maximum Subrectangle
http://codeforces.com/contest/1060/problem/C 题意: a.b数组长度分别为n.m.矩阵C,Cij=ai*bj.在C中找到一个子矩阵,该子矩阵所有元素和不大于 ...
- Codeforces Round #513
A. Phone Numbers 题意:给一些数字,每个电话号码以8开头,11位,求最多组成多少个号码,重复累加. #include <bits/stdc++.h> using names ...
- Codevs No.3147 矩阵乘法2
2016-06-01 17:33:30 题目链接: 矩阵乘法2 (Codevs No.3147) 题目大意: 给定两个大小相同的正方形矩阵A,B.多次询问,每次求乘后矩阵的一个子矩阵所有元素的和. 解 ...
随机推荐
- 触发器Demo
--mysql 触发器简单实例 --创建表1 )) ; --创建表2 )); --创建触发器,表一增加数据时,表二自动增加数据 create trigger t_afterinsert_on_tab1 ...
- LINUX vim 修改文件 退出
vim 保存退出, 先按ESC ,然后:wq(保存退出)W:write,写入 Q:quit,退出, 也可以直接输入X,代表WQ,也是保存退出 或者 先按ESC,再按shift+ZZ 也是保存退出 正常 ...
- Git的安装和使用(Linux)【转】
转自:http://my.oschina.net/fhd/blog/354685 Git诞生于Linux平台并作为版本控制系统率先服务于Linux内核,因此在Linux上安装Git是非常方便的.可以通 ...
- parseObject方法将json字符串转换成Map
String nwVal=recordDO.getWorkOrderNwVal(); HashMap<String,WxhcWorkOrderDO> nwMap=JSON.parseObj ...
- ifdef等宏的妙用
条件编译 一般情况下,源程序中所有的行都参加编译.但是有时希望对其中一部分内容只在满足一定条件才进行编译,也就是对一部分内容指定编译的条件,这就是"条件编译". 条件编译命令最常见 ...
- vue实现结算淘宝购物车效果
实现单选时的价格,全选时价格 单选效果图 全选效果图 html <template> <!-- 淘宝结算购物车 --> <div class="settleme ...
- ASP .NET Core 2.0 MVC 发布到 IIS 上以后 无法下载apk等格式的文件
ASP .NET Core MVC 发布到 IIS 上以后 无法下载apk等格式的文件 使用.NET Core MVC创建了一个站点,其他文件可以下载,但是后来又需求,就把手机端的apk合适的文件上 ...
- C++ 必须使用初始化列表
继承关系中,父类无默认构造函数 类类型类成员变量无默认构造函数 const类型成员变量 引用类型成员变量 不使用初始化列表,在创建对象调用构造函数之前会对所有的成员变量进行默认初始化,然后再执行构造函 ...
- javaweb笔记一
内连接(自然连接): 只有两个表相匹配的行才能在结果集中出现 外连接: 包括 (1)左外连接(左边的表不加限制) (2)右外连接(右边的表不加限制) (3)全外连接(左右两表都不加限制 一个空的构造器 ...
- LeetCode828. Unique Letter String
https://leetcode.com/problems/unique-letter-string/description/ A character is unique in string S if ...