# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
501364 | kevinxiehk | Gap (APIO16_gap) | C++17 | 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.
#ifdef __cplusplus
extern "C" {
#endif
void MinMax(long long s, long long t, long long* mn, long long* mx);
long long findGap(int T, int N);
#ifdef __cplusplus
}
#endif
// TODO: global variables can be declared here
long long max(long long a, long long b) {
return (a > b ? a : b);
}
long long findGap(int T, int n) {
// TODO: implementation
if(T == 1) {
long long arr[n + 5];
for(int i = 0; i < (n + 1) / 2; i++) {
if(i == 0) MinMax(0, 1000000000000000000LL, &arr[0], &arr[n - 1]);
else MinMax(arr[i - 1] + 1, arr[n - i] - 1, &arr[i], &arr[n - i - 1]);
}
long long ans = 0;
for(int i = 0; i < n - 1; i++) ans = max(ans, arr[i + 1] - arr[i]);
return ans;
}
else {
long long l, r;
MinMax(0, 1000000000000000000LL, &l, &r);
long long chunk = (r - l + n - 2) / (n - 1);
long long a, b, c = l;
l++;
long long ans = 0;
while(l < r) {
MinMax(l, l + chunk - 1, &a, &b);
l += chunk;
if(a == -1) continue;
ans = max(ans, a - c);
c = b;
}
ans = max(ans, r - c);
return ans;
}
}
#ifndef ONLINE_JUDGE
#include <cstdio>
#include <cmath>
int subtask_num, N;
long long A[100001];
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;
if (s > t) { lo /= 0; }
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;
}
int main() {
scanf("%d %d", &subtask_num, &N);
for (int i = 1; i <= N; i++) {
scanf("%lld", &A[i]);
}
printf("%lld\n", findGap(subtask_num, N));
printf("%lld\n", call_count);
return 0;
}
#endif