题干:

You are given two arrays aa and bb of positive integers, with length nn and mmrespectively.

Let cc be an n×mn×m matrix, where ci,j=ai⋅bjci,j=ai⋅bj.

You need to find a subrectangle of the matrix cc such that the sum of its elements is at most xx, and its area (the total number of elements) is the largest possible.

Formally, you need to find the largest number ss such that it is possible to choose integers x1,x2,y1,y2x1,x2,y1,y2 subject to 1≤x1≤x2≤n1≤x1≤x2≤n, 1≤y1≤y2≤m1≤y1≤y2≤m, (x2−x1+1)×(y2−y1+1)=s(x2−x1+1)×(y2−y1+1)=s, and

∑i=x1x2∑j=y1y2ci,j≤x.∑i=x1x2∑j=y1y2ci,j≤x.

Input

The first line contains two integers nn and mm (1≤n,m≤20001≤n,m≤2000).

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤20001≤ai≤2000).

The third line contains mm integers b1,b2,…,bmb1,b2,…,bm (1≤bi≤20001≤bi≤2000).

The fourth line contains a single integer xx (1≤x≤2⋅1091≤x≤2⋅109).

Output

If it is possible to choose four integers x1,x2,y1,y2x1,x2,y1,y2 such that 1≤x1≤x2≤n1≤x1≤x2≤n, 1≤y1≤y2≤m1≤y1≤y2≤m, and ∑x2i=x1∑y2j=y1ci,j≤x∑i=x1x2∑j=y1y2ci,j≤x, output the largest value of (x2−x1+1)×(y2−y1+1)(x2−x1+1)×(y2−y1+1) among all such quadruplets, otherwise output 00.

Examples

Input

3 3
1 2 3
1 2 3
9

Output

4

Input

5 1
5 4 2 4 5
2
5

Output

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,他们能组成一个矩阵,然后求子矩阵的和<=X的,最大的子矩阵的面积。

解题报告:

听说有人往滑窗问题上想了?

如果题目规定是正方形就好做了啊,二维前缀和+二分宽度,直接o(nmlog(nm)),如果矩形的话只能带俩log了。。。没尝试过。但应该也不难写。

想想啊,为什么题目不直接给你一个矩阵,而是给你俩数组让你自己造矩阵?肯定是因为矩阵只是个幌子,真正要用到的还是那两个数组。所以我们推公式:

于是我们知道了和x比较的值只与连续子段和有关,且A和B这两者之间没关系(只是最后做一下乘法)所以算出每个长度的最小和,最后再o(nm)遍历一遍维护答案就行了。

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll INF = 0x3f3f3f3f;
ll a[2005],b[2005],suma[2005],sumb[2005];
ll dpa[2005],dpb[2005];
ll x,ans;
int main()
{int n,m;cin>>n>>m;memset(dpa,INF,sizeof dpa);memset(dpb,INF,sizeof dpb);for(int i = 1; i<=n; i++) scanf("%lld",a+i),suma[i] = suma[i-1] + a[i];for(int i = 1; i<=m; i++) scanf("%lld",b+i),sumb[i] = sumb[i-1] + b[i];  scanf("%lld",&x);for(int len = 1; len<=n; len++) {for(int i = 1; i+len-1<=n; i++) {dpa[len] = min(dpa[len],suma[i+len-1] - suma[i-1]);}}for(int len = 1; len<=m; len++) {for(int i = 1; i+len-1<=m; i++) {dpb[len] = min(dpb[len],sumb[i+len-1] - sumb[i-1]);} }for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(dpa[i] * dpb[j] <= x) ans = max(ans,(ll)i*j);}}printf("%lld\n",ans);return 0 ;
}

网络版AC代码:(博客链接)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=2000;
ll lA,lB,x;
int A[N+5],B[N+5];
struct NODE{ll len,sum;
}node[N*N+5];
int cnt_NODE;
bool cmp_NODE(NODE A,NODE B){return A.sum<B.sum;
}
int Solve(ll sum){int l=0,r=cnt_NODE-1;if(node[r].sum<=sum) return r;while(l+1<r){int mid=(l+r)>>1;if(node[mid].sum<=sum) l=mid;else r=mid;}if(node[r].sum<=sum) return r;return l;
}
int main(){scanf("%d%d",&lA,&lB);for(int i=1;i<=lA;i++)scanf("%d",&A[i]);for(int i=1;i<=lB;i++)scanf("%d",&B[i]);scanf("%d",&x);for(int i=1;i<=lA;i++){ll sum=0;for(int j=i;j<=lA;j++){sum+=A[j];node[cnt_NODE].len=j-i+1;node[cnt_NODE].sum=sum;cnt_NODE++;}}sort(node,node+cnt_NODE,cmp_NODE);for(int i=1;i<cnt_NODE;i++)node[i].len=max(node[i].len,node[i-1].len);ll ans=0;for(int i=1;i<=lB;i++){ll sum=0;for(int j=i;j<=lB;j++){sum+=B[j];if(x/sum==0) continue;int res=Solve(x/sum);if(node[res].sum>x/sum) continue;ans=max(ans,node[res].len*(j-i+1));}}printf("%lld\n",ans);return 0;
}

【CodeForces - 1060C】Maximum Subrectangle (思维,预处理前缀和,dp,枚举长度)相关推荐

  1. Codeforces 484B Maximum Value(高效+二分)

    题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...

  2. 【CodeForces 332B --- Maximum Absurdity】递推

    [CodeForces 332B --- Maximum Absurdity]递推 题目来源:点击进入[CodeForces 332B - Maximum Absurdity] Description ...

  3. [CodeForces 332B]Maximum Absurdity[DP]

    题目链接: [CodeForces 332B]Maximum Absurdity[DP] 题意分析: 寻找两个不重叠的长度为k的子串,使得它们之和最大. 解题思路: 第一想法是,处理出从这个点开始,长 ...

  4. CodeForces 332B Maximum Absurdity(线段树单点更新)

    题意: 给你一个序列,找两个长度为 k 且没有重合区间的数使得其和最大 解析: 线段树,就是把起点为 i 长度为 k 的和预处理出来,再枚举a,与a线段不重合的,后面的部分用线段树来找最大位置,总复杂 ...

  5. 随机生成防伪码 带有前缀 可自行设置长度

    <?php /* 自动生成防伪码 带有前缀 可自行设置长度 */ $length = 10; $result = codingone($length); echo $result; functi ...

  6. HDU - 4856 Tunnels (预处理+状压dp)

    HDU - 4856 Tunnels (预处理+状压dp) [hud链接] [vj链接] 题目 Problem Description Bob is travelling in Xi'an. He f ...

  7. 【CodeForces - 632B】Alice, Bob, Two Teams (预处理,思维,前缀和后缀和)

    题干: Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. The ...

  8. Codeforces 332B Maximum Absurdity(DP+前缀和处理)

    题目链接:http://codeforces.com/problemset/problem/332/B 题目大意: 给你n个数和一个整数k,要求找到不相交的两个长度为k的区间,使得区间和最大,输出这两 ...

  9. Maximum Subrectangle(矩阵,前缀和)

    You are given two arrays aa and bb of positive integers, with length nn and mm respectively. Let cc ...

最新文章

  1. Spring Integration学习资料
  2. 双系统安装 Windows8和Windows Server2012
  3. Linux文件预读对系统的影响
  4. html5的消息通知
  5. 图像纹理合成_EnhanceNet:通过自动纹理合成实现单图像超分辨率
  6. 前端处理跨域的几种方式
  7. 使用kibana和elasticsearch日志实时绘制图表
  8. vsftpd安装问题汇总(持续更新。。)
  9. 面试题,你做了哪些事情来提升自己的沟通能力?
  10. IIS PHP MYSQL出现问题,IIS 运行PHP 正常使用MYSQL 解决报错0x000000ff
  11. csv文件怎么转成excel_Python操作Excel文件(1):花式大师pyexcel
  12. ubuntu 12.04 LTS 安装配置JDK1.6.0_45
  13. ❤️六万字《SpringMVC框架介绍—从入门到高级》(建议收藏)❤️
  14. ASP.NET使用UpdatePanel实现AJAX
  15. ADT(Android) — Eclipse开发NOX夜神安卓模拟器如何进行横竖屏切换
  16. 新建mavan项目转为web项目
  17. Java笔记(2)黑马刘意_Java入门_2019_idea版
  18. 宾馆管理系统java外文文献_javaEE酒店管理系统论文+任务书+设计源码+答辩PPT
  19. Bootstrap3引入相关知识点
  20. centos7 yum 配置阿里云镜像

热门文章

  1. nlp中的经典模型(三)
  2. ubuntu eclipse java,Ubuntu快速安装eclipse
  3. 通汇手机为何卖得那么红火
  4. 跨站脚本专题 XSS
  5. 1-5 三整数排序(算法竞赛入门经典)
  6. 7-4 统计工龄 (20 分)(C语言实现)
  7. php聊天功能逻辑原理,@xxx提及用户,这种功能的实现原理是怎样的?
  8. 非线性时延系统matlab框图,非线性主-从时延系统的时滞相关有限时间同步控制方法与流程...
  9. python字符串格式化_Python3 字符串格式化
  10. Linux TCP server系列(6)-select模式下的多线程server