2021牛客暑期多校训练营4 J. Average(最长连续子序列平均值)
链接:https://ac.nowcoder.com/acm/contest/11255/J
来源:牛客网
题目描述
Bob has an n×mn×m matrix WW.
This matrix is very special, It's calculated by two sequences a1...n,b1...ma1...n,b1...m, ∀i∈[1,n],∀j∈[1,m]∀i∈[1,n],∀j∈[1,m], Wi,j=ai+bjWi,j=ai+bj
Now Bob wants to find a submatrix of WW with the largest average value.
Bob doesn't want the size of submatrix to be too small, so the submatrix you find must satisfy that the width(the first dimension of matrix) of it is at least xx and the height(the second dimension of matrix) of it is at least yy.
Now you need to calculate the largest average value.
输入描述:
The first line has four integers n,m,x,yn,m,x,y.
The second line has nn integers a1...na1...n.
The third line has mm integers b1...mb1...m.
1≤n,m≤1051≤n,m≤105
1≤x≤n,1≤y≤m1≤x≤n,1≤y≤m
0≤ai,bi≤1050≤ai,bi≤105
输出描述:
Output the largest average value.
Your answer will be considered correct if the absolute or relative error is less than 10−610−6
示例1
输入
复制
3 4 2 2
3 1 2
4 1 3 2
输出
复制
4.6666666667
不妨设选择的那块矩形长为,宽为,对应的行列的起始位置分别为,则平均值为,其中为a数组的到位置的和。最大化均值相当于分别求a、b两个数组的连续子区间的最大均值然后再求和。而这个问题可以参考poj2018https://blog.csdn.net/weixin_43191865/article/details/90612344
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y;
double a[100005], b[100006];
double sum1[100005], sum2[100005];
int main() {
cin >> n >> m >> x >> y;
double ans1 = 0, ans2 = 0;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
for(int i = 1; i <= m; i++) {
cin >> b[i];
}
double l=-1e7;
double r=1e7;
while(r-l>1e-9)
{
double mid=(l+r)/2;
for(int i=1;i<=n;i++)
{
sum1[i]=sum1[i-1]+a[i]-mid;
}
double minn=1e9;
double maxx=-1e9;
for(int i=x;i<=n;i++)//这里多想一下就明白了。
{
minn=min(sum1[i-x],minn);
maxx=max(sum1[i]-minn,maxx);
}
if(maxx>0)
{
l=mid;
}
else
{
r=mid;
}
}
ans1 = r;
l=-1e7;
r=1e7;
while(r-l>1e-9)
{
double mid=(l+r)/2;
for(int i=1;i<=m;i++)
{
sum2[i]=sum2[i-1]+b[i]-mid;
}
double minn=1e9;
double maxx=-1e9;
for(int i=y;i<=m;i++)
{
minn=min(sum2[i-y],minn);
maxx=max(sum2[i]-minn,maxx);
}
if(maxx>0)
{
l=mid;
}
else
{
r=mid;
}
}
ans2 = r;
cout << fixed << setprecision(8) << ans1 + ans2;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2020-07-26 外一章