# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
243609 | uacoder123 | Gap (APIO16_gap) | C++14 | 0 ms | 0 KiB |
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 <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define FOR(i,a,b) for (auto i = (a); i <= (b); ++i)
#define NFOR(i,a,b) for(auto i = (a); i >= (b); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define mp(i,a) make_pair(i,a)
#define pb(a) push_back(a)
#define bit(x,b) (x&(1LL<<b))
typedef long long int lli;
typedef pair <lli,lli> ii;
typedef pair <lli,ii> iii;
typedef vector <lli> vi;
static void my_assert(int k){ if (!k) exit(1); }
static int subtask_num, N;
static long long A[100001];
static long long call_count;
void MinMax(long long s, long long t, long long *mn, long long *mx)
{
int lo = 1, hi = N, left = N+1, right = 0;
my_assert(s <= t && mn != NULL && mx != NULL);
while (lo <= hi){
int mid = (lo+hi)>>1;
if (A[mid] >= s) hi = mid - 1, left = mid;
else lo = mid + 1;
}
lo = 1, hi = N;
while (lo <= hi){
int mid = (lo+hi)>>1;
if (A[mid] <= t) lo = mid + 1, right = mid;
else hi = mid - 1;
}
if (left > right) *mn = *mx = -1;
else{
*mn = A[left];
*mx = A[right];
}
if (subtask_num == 1) call_count++;
else if (subtask_num == 2) call_count += right-left+2;
}
long long findGap(int T, int N)
{
lli c=0,arr[N],l=0,rem=0,g,r=1000000000000000000,l1=0,r1=N-1,ans1,ans2,m=0;
if(T==1)
{
for(;l1<=r1;)
{
MinMax(l,r,&ans1,&ans2);
arr[l1]=ans1;
l1++;
l=ans1+1;
arr[r1]=ans2;
r1--;
r=ans2-1;
}
for(lli i=0;i<N-1;++i)
m=max(m,arr[i+1]-arr[i]);
}
else
{
MinMax(l,r,&ans1,&ans2);
l=ans1+1;
r=ans2-1;
g=(r-l+1)/(N-1);
rem+=(r-l+1)%(N-1);
r1=g+l-1;
vector<ii> v;
v.pb(mp(ans1,ans1));
while(1)
{
MinMax(l,min(r1+min(1*1LL,rem),r),&ans1,&ans2);
if(r<=min(r1+min(1*1LL,rem),r))
break;
l=1+r1+min(1*1LL,rem);
r1=r1+min(1*1LL,rem)+g;
if(rem>0)
rem-=1;
if(ans1!=-1)
v.pb(mp(ans1,ans2));
}
v.pb(mp(r+1,r+1));
for(lli i=0;i<lli(v.size())-1;++i)
{
m=max(m,v[i+1].F-v[i].S);
}
}
return m;
}
int main()
{
FILE *in = stdin, *out = stdout;
my_assert(2 == fscanf(in, "%d%d", &subtask_num, &N));
my_assert(1 <= subtask_num && subtask_num <= 2);
my_assert(2 <= N && N <= 100000);
for (int i=1;i<=N;i++) my_assert(1 == fscanf(in, "%lld", A+i));
for (int i=1;i<N;i++) my_assert(A[i] < A[i+1]);
fprintf(out, "%lld\n", findGap(subtask_num, N));
fprintf(out, "%lld\n", call_count);
}