Submission #950923

# Submission time Handle Problem Language Result Execution time Memory
950923 2024-03-21T02:19:14 Z kilkuwu Gap (APIO16_gap) C++17
30 / 100
43 ms 4004 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);
    dbg(lo, hi);

    // 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);

      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) {
  dbg("called ", T, 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 0 ms 2392 KB Output is correct
3 Correct 1 ms 2392 KB Output is correct
4 Correct 1 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 0 ms 2488 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 2580 KB Output is correct
17 Correct 7 ms 2584 KB Output is correct
18 Correct 7 ms 2548 KB Output is correct
19 Correct 7 ms 2584 KB Output is correct
20 Correct 6 ms 2568 KB Output is correct
21 Correct 28 ms 3624 KB Output is correct
22 Correct 28 ms 3620 KB Output is correct
23 Correct 28 ms 3620 KB Output is correct
24 Correct 29 ms 3620 KB Output is correct
25 Correct 32 ms 3624 KB Output is correct
26 Correct 29 ms 3440 KB Output is correct
27 Correct 33 ms 3624 KB Output is correct
28 Correct 28 ms 3620 KB Output is correct
29 Correct 28 ms 3612 KB Output is correct
30 Correct 23 ms 3616 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 1 ms 2644 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 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 11 ms 2840 KB Output is correct
17 Correct 12 ms 2828 KB Output is correct
18 Correct 10 ms 2844 KB Output is correct
19 Correct 11 ms 2832 KB Output is correct
20 Runtime error 5 ms 2600 KB Execution failed because the return code was nonzero
21 Correct 40 ms 3980 KB Output is correct
22 Correct 40 ms 3988 KB Output is correct
23 Correct 39 ms 4004 KB Output is correct
24 Correct 40 ms 3996 KB Output is correct
25 Correct 41 ms 3932 KB Output is correct
26 Correct 43 ms 3976 KB Output is correct
27 Correct 42 ms 3888 KB Output is correct
28 Correct 40 ms 3984 KB Output is correct
29 Correct 43 ms 3940 KB Output is correct
30 Runtime error 24 ms 3116 KB Execution failed because the return code was nonzero
31 Correct 1 ms 2392 KB Output is correct
32 Correct 1 ms 2392 KB Output is correct