0% found this document useful (0 votes)
7 views2 pages

Peti 2

The document contains a C++ program that reads two arrays and computes the maximum product of lengths of subarrays from both arrays, constrained by a given threshold. It utilizes prefix sums and binary search to efficiently find the optimal pairs of subarrays. The main function executes the tests function, which performs the calculations and outputs the result.

Uploaded by

ilina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Peti 2

The document contains a C++ program that reads two arrays and computes the maximum product of lengths of subarrays from both arrays, constrained by a given threshold. It utilizes prefix sums and binary search to efficiently find the optimal pairs of subarrays. The main function executes the tests function, which performs the calculations and outputs the result.

Uploaded by

ilina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <bits/stdc++.

h>
#define endl '\n'
#define pb push_back
#define ll long long
#define fi first
#define se second
#define pll pair<ll,ll>

using namespace std;

void tests()
{
ll n,m,i,sab,oduz,res=-1,j,sza,szb,val,siz,mid,l,r,f;
cin >> n >> m;
vector<ll>a(n),b(m);
for(auto &x:a)
{cin >>x;}
for(auto &x:b)
{cin>>x;}
vector<pll>pa,pb;
oduz=0;
cin >> f;
for(i=0;i<n;i++)
{
sab=0;
for(j=i;j<n;j++)
{
sab+=a[j];
pa.pb({j-i+1ll,sab});
}
}
for(i=0;i<m;i++)
{
sab=0;
for(j=i;j<m;j++)
{
sab+=b[j];
pb.pb({j-i+1ll,sab});
}
}
//sort(pa.begin(),pa.end());
sort(pb.begin(),pb.end());
sza=pa.size();
szb=pb.size();
for(i=szb-2;i>=0;i--)
{
if(pb[i].se>=pb[i+1].se)
{
pb[i]=pb[i+1];
}
}

for(auto &x:pa)
{
// cout << x.fi << " " << x.se << "?" << endl;
val=x.se;
siz=x.fi;
l=0ll;
r=szb-1;
while(l<=r)
{
mid=(l+r)/2;
if(pb[mid].se*val<=f)
{
l=mid+1;
res=max(res,pb[mid].fi*siz);
}
else
{
r=mid-1;
}

}
}
cout << res << endl;
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tt=1;
// cin >> tt;
while(tt--)
{
tests();
}
}

You might also like