This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "gap.h"
#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std;
long long findGap(int T, int N)
{
int cnt=N;
ll A=1e18;
ll ans=0;
ll mn,mx;
MinMax((ll)0,A,&mn,&mx);
if(N==2)return mx-mn;
if(N==3||N==4)
{
ll mn1,mx1;
MinMax(mn+1,mx-1,&mn1,&mx1);
return max(max(mn1-mn,mx-mx1),mx1-mn1);
}
ans=(mn-mx-1)/(N-1)+1;
queue<pair<ll,ll>> q;
q.push({mn,mx});
while(cnt>0&&!q.empty())
{
ll l=q.front().fi,r=q.front().se;
q.pop();
if(l+1==r||l==r)
{
ans=max(ans,r-l);
continue;
}
ll mid=(l+r)>>1;
ll mn1,mx1;
MinMax(l,mid,&mn1,&mx1);
ll mn2,mx2;
MinMax(mid+1,r,&mn2,&mx2);
if(mn1==-1&&mx1==-1&&mn2==-1&&mx2==-1)ans=max(ans,r-l);
if(mn1!=-1)
{
cnt--;
if(mn1!=mx2)cnt--;
}
if(mn2!=-1)
{
cnt--;
if(mn2!=mx2)cnt--;
}
if(mx1!=-1&&mn2!=-1)ans=max(ans,mn2-mx1);
if(mn1!=-1)ans=max(ans,mn1-l);
else if(mn2!=-1)ans=max(ans,mn2-l);
if(mx2!=-1)ans=max(ans,r-mx2);
else if(mx1!=-1)ans=max(ans,r-mx1);
if(mn1!=mx1&&mx1-mn1>ans)q.push({mn1,mx1});
if(mn2!=mx2&&mx2-mn2>ans)q.push({mn2,mx2});
}
return (ll)ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |