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.多次询问,每次求乘后矩阵的一个子矩阵所有元素的和. 解 ...
 
随机推荐
- ubuntu更新源列表
			
1. 备份源列表 sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup 2.修改更新源 打开源列表 sudo gedit /etc/ap ...
 - 5 - django-csrf-session&cookie
			
目录 1 CSRF跨站请求伪造 1.1 CSRF攻击介绍及防御 1.2 防御CSRF攻击 1.2.1 验证 HTTP Referer 字段 1.2.2 在请求地址中添加 token 并验证 1.2.3 ...
 - go 流程控制
			
if else 语句 基本语法 if condition { //do something } if condition { //do something } else if condition { ...
 - Cloud Lab: 泰晓实验云台【转】
			
转自:http://tinylab.org/cloud-lab/ 可快速构建的计算机课程在线实验平台 由 Wu Zhangjin 创建于 2017/10/06 评论 打赏 项目描述 泰晓实验云台 项目 ...
 - asp.net操作word 配置在IIS上出现的问题
			
异常: 检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问. (异常来自 ...
 - HDU 6203 2017沈阳网络赛 LCA,DFS+树状数组
			
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6203 题意:n+1 个点 n 条边的树(点标号 0 ~ n),有若干个点无法通行,导致 p 组 U V ...
 - scrapy shell命令的【选项】简介
			
在使用scrapy shell测试某网站时,其返回400 Bad Request,那么,更改User-Agent请求头信息再试. DEBUG: Crawled () <GET https://w ...
 - 170406回顾-SQL Server的smalldatetime类型比较
			
在比较SQL Server的类型为smalldatetime字段时出现下面的错误:将 expression 转换为数据类型 smalldatetime 时出现算术溢出错误 正确的比较方法如下:将lon ...
 - R语言以及RStdio的安装
			
R语言: 首先从官网上下载R安装包, 提供了Linux, (Mac) OS X, Windows的安装包相关下载链接. RStdio: RStdio(官网)是R言语非常实用的IDE, 是一个免费的软件 ...
 - SSIS 学习之旅 FTP访问类
			
这章把脚本任务访问FTP的方法 全部给大家. 控件的使用大家如果有不懂得可以看下我之前的文章.第一章:SSIS 学习之旅 第一个SSIS 示例(一)(上) 第二章:SSIS 学习之旅 第一个SSIS ...