zoukankan      html  css  js  c++  java
  • 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
    

    不妨设选择的那块矩形长为(x),宽为(y),对应的行列的起始位置分别为(x_l,x_r,y_l,y_r),则平均值为(average = frac{ySum[x_l...x_r]+xSum[y_l...y_r]}{xy}=frac{Sum[x_l...x_r]}{x}+frac{Sum[y_l...y_r]}{y}),其中(Sum[x_l...x_r])为a数组的(x_l)(x_r)位置的和。最大化均值相当于分别求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;
    }
    
  • 相关阅读:
    自增长主键Id的另类设计
    Android 混淆那些事儿
    H5 和移动端 WebView 缓存机制解析与实战
    快速上手 Kotlin 的 11 招
    教你 Debug 的正确姿势——记一次 CoreMotion 的 Crash
    小程序组件化框架 WePY 在性能调优上做出的探究
    基于 TensorFlow 在手机端实现文档检测
    HTTPS 原理浅析及其在 Android 中的使用
    Bugly 多渠道热更新解决方案
    Swift 对象内存模型探究(一)
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/15062880.html
Copyright © 2011-2022 走看看