Cod sursa(job #2333553)

Utilizator patcasrarespatcas rares danut patcasrares Data 1 februarie 2019 13:58:01
Problema Balans Scor 40
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.53 kb
#include<bits/stdc++.h>
#define pb push_back
#define x first
#define y second
using namespace std;
ifstream fin("balans.in");
ofstream fout("balans.out");
const int DN=305;
int m,n,l,u;
int R,C;
double st,dr,mij,sum[DN],val,b[DN][DN],sol;
pair<double,double>a[DN];
deque<pair<double,int> >d;
int vf(double z)
{
	for(int i=1;i<=n;i++)
	{
		val=a[i].x-a[i].y*z;
		sum[i]=sum[i-1]+val;
	}
	d.clear();
	for(int i=1;i<=n;i++)
	{
		if(i>=l)
		{
			while(1)
			{
				if(d.empty())
					break;
				if(sum[i-l]>d.back().x)
					break;
				d.pop_back();
			}
			d.pb({sum[i-l],i});
		}
		while(1)
		{
			if(d.empty())
				break;
			if(d.front().y>=i-u)
				break;
			d.pop_front();
		}
		if(!d.empty())
			if(sum[i]-d.front().x>=0)
				return 1;
	}
	return 0;
}
int main()
{
	fin>>m>>n;
	fin>>R>>C;
	l=C;
	u=n;
	for(int i=1;i<=m;i++)
		for(int j=1;j<=n;j++)
			fin>>b[i][j];
	for(int i=1;i<=m;i++)
		for(int j=n+1;j<=2*n;j++)
			b[i][j]=b[i][j-n];
	n*=2;
	for(int i=m+1;i<=2*m;i++)
		for(int j=1;j<=n;j++)
			b[i][j]=b[i-m][j];
	m*=2;
	for(int i=1;i<=m;i++)
		for(int j=1;j<=n;j++)
			b[i][j]+=b[i-1][j];
	for(int i1=1;i1<=m;i1++)
		for(int i2=i1+R-1;i2<=min(m,i1-1+m/2);i2++)
		{
			for(int i=1;i<=n;i++)
			{
				a[i].y=1;
				a[i].x=b[i2][i]-b[i1-1][i];
				a[i].x/=i2-i1+1;
			}
			st=0;
			dr=1e5;
			while(dr-st>1e-4)
			{
				mij=(st+dr)/2;
				if(vf(mij))
					st=mij;
				else
					dr=mij;
			}
			st=(st+dr)/2;
			sol=max(sol,st);
		}
	fout<<fixed<<setprecision(3)<<sol;
}