Average
题意:
矩阵W的值可以通过数组a和b得到,W[i][j]=a[i]+b[j],现在求W的一个子矩阵,平均值最大,且子矩阵必须满足宽度至少是x,高度至少是y,计算最大平均值
题解:
那答案就变成了分别对a和b对应的区间求个平均值然后相加
问题就变成了,找a的一个长度至少为x的平均值的子矩阵和b的一个长度至少为y的平均值最大的子矩阵
二分平均值S,然后令a[i]编程a[i]-S,然后看是否有和>0的长度至少为x的子矩阵
总复杂度为O(nlogW)
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
int a[maxn],b[maxn],suma[maxn],sumb[maxn];
int n,m,x,y;
int pa,pb;
int amax,bmax,amin,bmin;
bool check( double mid,int k) {double sum = 0, prev = 0, min_sum = 0;for (int i = 0; i < k; i++)sum += a[i] - mid;if (sum >= 0)return true;for (int i = k; i < n; i++){sum += a[i] - mid;prev += a[i - k] - mid;min_sum = min(prev, min_sum);if (sum >= min_sum)return true;}return false;
}
bool check1( double mid,int k) {double sum = 0, prev = 0, min_sum = 0;for (int i = 0; i < k; i++)sum += b[i] - mid;if (sum >= 0)return true;for (int i = k; i < m; i++){sum += b[i] - mid;prev += b[i - k] - mid;min_sum = min(prev, min_sum);if (sum >= min_sum)return true;}return false;
}int main()
{cin>>n>>m>>x>>y;amin=inf;bmin=inf;for(int i=0;i<n;i++){cin>>a[i];suma[i]=suma[i-1]+a[i];amax=max(amax,a[i]);amin=min(amin,a[i]);}for(int i=0;i<m;i++){cin>>b[i];sumb[i]=sumb[i-1]+b[i];bmax=max(bmax,b[i]);bmin=min(bmin,b[i]);}//cout<<amin<<amax<<endl;double ans=0;double l=(double)amin;double r=(double)amax;while(r-l>0.00000001){//cout<<l<<endl;double mid=(l+r)/2.0;//cout<<mid<<endl;if(check(mid,x)){l=mid;}else r=mid;}ans+=l;//cout<<ans<<endl;l=(double)bmin;r=(double)bmax;while(r-l>0.00000001){double mid=(l+r)/2.0;if(check1(mid,y)){l=mid;}else r=mid;}ans+=l;printf("%.7lf\n",ans); return 0;
}