Submission #950927

# Submission time Handle Problem Language Result Execution time Memory
950927 2024-03-21T02:23:38 Z kilkuwu Gap (APIO16_gap) C++17
100 / 100
44 ms 4224 KB
#include <bits/stdc++.h>

#ifndef LOCAL
#include "gap.h"
#else

void MinMax(long long, long long, long long*, long long*);

static void my_assert(int k){ if (!k) exit(1); }


long long findGap(int, int);

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;
}

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);
}
#endif
#ifdef LOCAL
#include "template\debug.hpp"
#else
#define dbg(...) ;
#define timer(...) ;
#endif

struct Subtask1 {
  int n;

  Subtask1(int _n) : n(_n) {}

  long long solve() {
    std::vector<long long> a(n);

    MinMax(0, 1e18, &a[0], &a.back());

    for (int i = 1; i <= n - i - 1; i++) {
      MinMax(a[i - 1] + 1, a[n - i] - 1, &a[i], &a[n - i - 1]);
    }

    long long ans = a[1] - a[0];
    for (int i = 1; i + 1 < n; i++) {
      ans = std::max(ans, a[i + 1] - a[i]);
    }
    return ans;
  }
};


struct Subtask2 {
  int n;

  Subtask2(int _n) : n(_n) {}

  long long solve() {
    long long lo, hi;
    MinMax(0, 1e18, &lo, &hi);

    if (hi - lo + 1 == n) {
      return 1;
    }
    
    if (n == 2) {
      return hi - lo;
    }

    // we can do this 
  
    long long gap = (hi - lo - 1) / (n - 1);
    if (gap * (n - 1) < (hi - lo - 1)) gap++;
    dbg(gap);

    // we can do that now 
    std::vector<long long> values;
    values.push_back(lo);
    
    for (int i = 0; i < n - 1; i++) {
      long long ff = lo + 1 + gap * i;
      long long ee = lo + gap * (i + 1);
      ee = std::min(ee, hi - 1);

      if (ff > ee) continue;
      long long r1, r2;
      MinMax(ff, ee, &r1, &r2);
      if (r1 >= 0) {
        values.push_back(r1);
        if (r2 > r1) {
          values.push_back(r2);
        }
      }
    }

    values.push_back(hi);
    long long ans = 0;
    for (int i = 0; i + 1 < (int) values.size(); i++) {
      ans = std::max(ans, values[i + 1] - values[i]);
    }
    return ans;
  }
};

long long findGap(int T, int N) {
  if (T == 1) {
    Subtask1 sub1(N);
    return sub1.solve();
  } else {
    Subtask2 sub2(N);
    return sub2.solve();
  }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 0 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 0 ms 2392 KB Output is correct
7 Correct 1 ms 2488 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2392 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 1 ms 2392 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2392 KB Output is correct
16 Correct 8 ms 2588 KB Output is correct
17 Correct 7 ms 2580 KB Output is correct
18 Correct 8 ms 2588 KB Output is correct
19 Correct 7 ms 2584 KB Output is correct
20 Correct 6 ms 2576 KB Output is correct
21 Correct 28 ms 3604 KB Output is correct
22 Correct 28 ms 3628 KB Output is correct
23 Correct 28 ms 3620 KB Output is correct
24 Correct 28 ms 3556 KB Output is correct
25 Correct 25 ms 3620 KB Output is correct
26 Correct 28 ms 3620 KB Output is correct
27 Correct 36 ms 3624 KB Output is correct
28 Correct 28 ms 3624 KB Output is correct
29 Correct 34 ms 3620 KB Output is correct
30 Correct 23 ms 3620 KB Output is correct
31 Correct 1 ms 2392 KB Output is correct
32 Correct 1 ms 2392 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 0 ms 2392 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 0 ms 2392 KB Output is correct
5 Correct 1 ms 2392 KB Output is correct
6 Correct 1 ms 2392 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 0 ms 2392 KB Output is correct
10 Correct 1 ms 2392 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 1 ms 2392 KB Output is correct
13 Correct 1 ms 2392 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2392 KB Output is correct
16 Correct 10 ms 3008 KB Output is correct
17 Correct 11 ms 2840 KB Output is correct
18 Correct 10 ms 2844 KB Output is correct
19 Correct 10 ms 2840 KB Output is correct
20 Correct 5 ms 2556 KB Output is correct
21 Correct 40 ms 3984 KB Output is correct
22 Correct 44 ms 4224 KB Output is correct
23 Correct 40 ms 3996 KB Output is correct
24 Correct 40 ms 4000 KB Output is correct
25 Correct 38 ms 4028 KB Output is correct
26 Correct 41 ms 3988 KB Output is correct
27 Correct 44 ms 4008 KB Output is correct
28 Correct 41 ms 4000 KB Output is correct
29 Correct 41 ms 4004 KB Output is correct
30 Correct 22 ms 3104 KB Output is correct
31 Correct 0 ms 2392 KB Output is correct
32 Correct 1 ms 2392 KB Output is correct