Submission #1042917

#TimeUsernameProblemLanguageResultExecution timeMemory
1042917juicyBitaro's travel (JOI23_travel)C++17
0 / 100
257 ms5156 KiB
#include <bits/stdc++.h>

using namespace std;

#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif

const int N = 2e5 + 5;

int n, q;
int x[N];

int main() {
  ios::sync_with_stdio(false); cin.tie(nullptr);

  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> x[i];
  }
  cin >> q;
  while (q--) {
    int s; cin >> s;
    int r = lower_bound(x + 1, x + n + 1, s) - x;
    int l = r - (x[r] != s);
    long long res = 0;
    while (1) {
      if (l == 1) {
        res += abs(s - x[n]);
        break;
      }
      if (r == n) {
        res += abs(s - x[1]);
        break;
      }
      int L = x[l - 1], R = x[r + 1];
      if (s - L <= R - s) {
        int nxt = lower_bound(x + 1, x + n + 1, 2 * s - R) - x;
        res += abs(s - x[nxt]);
        s = x[nxt];
        l = nxt;
      } else {
        int nxt = upper_bound(x + 1, x + n + 1, 2 * s - L) - x - 1;
        res += abs(s - x[nxt]);
        s = x[nxt];
        r = nxt;
      }
    }
    cout << res << "\n";
  }
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...