当前位置:网站首页>Niuke 2021 summer training 4-j-average
Niuke 2021 summer training 4-j-average
2022-07-18 04:59:00 【yl-9】
Cattle guest 2021 Summer training 4-J-Average
The question
Given n*m Matrix ,w[i][j]=a[i]+b[j], Find one not less than x * y The submatrix of , Its average value is the largest
n,m,ai,bi≤105
Answer key

Suppose that the submatrix we require is
t o t = ∑ i = x 1 x 2 ∑ j = y 1 y 2 ( a i + b j ) = ( y 2 − y 1 + 1 ) ∗ ∑ i = x 1 x 2 a i + ( x 2 − x 1 + 1 ) ∗ ∑ j = y 1 y 2 b j tot=\sum_{i=x1}^{x2}\sum_{j=y1}^{y2}(a_i+b_j)=(y2-y1+1)*\sum_{i=x1}^{x2}a_i+(x2-x1+1)*\sum_{j=y1}^{y2}b_j tot=i=x1∑x2j=y1∑y2(ai+bj)=(y2−y1+1)∗i=x1∑x2ai+(x2−x1+1)∗j=y1∑y2bj
that
a v e = ( y 2 − y 1 + 1 ) ∗ ∑ i = x 1 x 2 a i + ( x 2 − x 1 + 1 ) ∗ ∑ j = y 1 y 2 b j ( x 2 − x 1 + 1 ) ∗ ( y 2 − y 1 + 1 ) = ∑ i = x 1 x 2 a i x 2 − x 1 + 1 + ∑ i = y 1 y 2 b j y 2 − y 1 + 1 ave=\frac{(y2-y1+1)*\sum_{i=x1}^{x2}a_i+(x2-x1+1)*\sum_{j=y1}^{y2}b_j}{(x2-x1+1)*(y2-y1+1)}=\frac{\sum_{i=x1}^{x2}a_i}{x2-x1+1}+\frac{\sum_{i=y1}^{y2}b_j}{y2-y1+1} ave=(x2−x1+1)∗(y2−y1+1)(y2−y1+1)∗∑i=x1x2ai+(x2−x1+1)∗∑j=y1y2bj=x2−x1+1∑i=x1x2ai+y2−y1+1∑i=y1y2bj
in other words , We just need to be right about a The array is continuous and its length is not less than x The maximum average of ,b Arrays are the same thing , Next, the key is how to find the maximum
Obviously, monotony can be considered . hypothesis q Medium is the average value of single subtraction , Traverse a i It can pop up q End less than with i Is the ending length is x Average value , To maintain monotony , Can prove that q The previous average plus the same value is still a single minus
DB Ave(int l,int r)
{
return (sum[r]-sum[l-1])/(DB)(r-l+1);
}
for(int i=x;i<=n;i++)
{
while(0<=t&&Ave(q[t],i)<Ave(i-x+1,i)) t--;
q[++t]=i-x+1;
ans1=max(ans1,Ave(q[0],i));
}
Code
#include<bits/stdc++.h>
#define LL long long
#define DB double
using namespace std;
const int N=1e5+9;
int n,m,x,y,t;
DB ans1,ans2;;
int q[N];
DB a[N],sum[N];
DB Ave(int l,int r)
{
return (sum[r]-sum[l-1])/(DB)(r-l+1);
}
int main()
{
cin>>n>>m>>x>>y;
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum[i]=sum[i-1]+a[i];
}
t=-1;
for(int i=x;i<=n;i++)
{
while(0<=t&&Ave(q[t],i)<Ave(i-x+1,i)) t--;
q[++t]=i-x+1;
ans1=max(ans1,Ave(q[0],i));
}
memset(sum,0,sizeof(sum));
for(int i=1;i<=m;i++)
{
cin>>a[i];
sum[i]=sum[i-1]+a[i];
}
t=-1;
memset(q,0,sizeof(q));
for(int i=y;i<=m;i++)
{
while(0<=t&&Ave(q[t],i)<Ave(i-y+1,i)) t--;
q[++t]=i-y+1;
ans2=max(ans2,Ave(q[0],i));
}
printf("%.10lf",ans1+ans2);
return 0;
}
边栏推荐
- 简化IT供应商采用流程的六种方式
- 【SpaceNet】SN6:Multi-Sensor All-Weather Mapping
- LeetCode_ Sliding window_ Binary search_ Medium_ 713. Subarray with product less than k
- A solution to the failure of NVIDIA SMI command
- 普歌-码上鸿鹄团队-Vscode 代码格式化整理
- u-boot之顶层Makefile分析(三)
- 角色授权---通过添加和删除一级菜单,完成二级菜单的添加和删除
- Motion capture assists China Electric Power Research Institute in establishing a side cloud collaborative power independent inspection system
- 云中断的三个主要原因
- Winform屏幕截图保存C#代码
猜你喜欢
随机推荐
【luogu P4183】Cow at Large P(点分治)(图论)(树状数组)
[QT] operation of Jason in QT
云中断的三个主要原因
【SpaceNet】SN6:Multi-Sensor All-Weather Mapping
Top level makefile analysis of u-boot (3)
Dynamic memory functions and common dynamic memory errors
Dream CMS foreground SQL injection
LeetCode_滑动窗口_二分搜索_中等_713.乘积小于 K 的子数组
ASTM E595-15(2021) Outgassing除气测试最新标准
u-boot之顶层Makefile分析(二)之config.mk文件的生成
牛客2021暑期训练5-B-Boxes
【Flutter--实战】Flutter 简介
1、 MySQL Foundation
2022年宁德市职业院校教师实践教学能力提升培训——网络搭建与管理
使用vscode搭建u-boot开发环境
Trees and binary trees
三、索引优化
Cookie与Session
SSM整合
LeetCode_ Sliding window_ Simple_ 643. Maximum average number of subarrays I









