# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
396232 | giorgikob | Gap (APIO16_gap) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
using namespace std;
//#include "game.h"
//#include "gap.h"
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;
long long findGap(int, int);
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;
}
ll answer = 0;
long long findGap(int T, int N)
{
ll l = -1, r = 1e18; r++;
ll cnt = 0;
int calls = 0;
ll len = -1;
if(T == 1){
while(true){
ll new_l,new_r;
if(len != -1){
if((r-l) < len/N) break;
}
cnt++;
if(answer >= r-l) break;
if(cnt > (N+1)/2){
if(l != r){
answer = max(answer,r-l);
}
break;
}
if(l+1 > r-1){
if(l == r) break;
answer = max(answer,r-l);
break;
}
MinMax(l+1,r-1,&new_l,&new_r);
calls++;
if(new_l == -1){
answer = max(answer,r-l);
break;
} else {
if(cnt > 1)answer = max(answer, max(new_l-l,r-new_r)); else len = new_r-new_l;
l = new_l;
r = new_r;
}
}
} else {
ll last = -1;
MinMax(0,1e18,&l,&r);
ll k = (r-l+1)/N;
if((r-l+1)%N) k++;
if(r-l+1 == N){
return N;
}
assert(k != 1);
for(ll i = l; i <= r; i += k){
ll a,b;
ll askl = i;
if(i == l) askl = l+1;
MinMax(askl,min(i+k-1,r),&a,&b);
if(a == -1){
continue;
}
//answer = max(answer,b-a);
if(last != -1){
answer = max(answer,a-last);
}
last = b;
}
}
return answer;
}
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);
//assert(call_count <= (N+1)/2);
}