Submission #392750

# Submission time Handle Problem Language Result Execution time Memory
392750 2021-04-21T14:45:18 Z usachevd0 Shortcut (IOI16_shortcut) C++14
0 / 100
1 ms 332 KB
#pragma gcc optimize("Ofast")
#pragma gcc optimize("O3")
#pragma gcc optimize("no-stack-protector")
#pragma gcc optimize("unroll-loops")
#pragma gcc optimize("fast-math")
#pragma gcc target("avx,avx2,sse,sse2,sse3,ssse3,sse4,ffa")
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <numeric>
#ifndef DEBUG
  #include "shortcut.h"
#endif

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
/*template<typename T1, typename T2> bool chkmin(T1& x, T2 y) {
  return y < x ? (x = y, true) : false;
}
template<typename T1, typename T2> bool chkmax(T1& x, T2 y) {
  return y > x ? (x = y, true) : false;
}*/
void chkmin(ll& x, ll y) {
  if (y < x) {
    x = y;
  }
}
void chkmax(ll& x, ll y) {
  if (y > x) {
    x = y;
  }
}
void debug_out() {
  cerr << endl;
}
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) {
  cerr << ' ' << A;
  debug_out(B...);
}
template<typename T> void mdebug_out(T* a, int n) {
  for (int i = 0; i < n; ++i) {
    cerr << a[i] << ' ';
  }
  cerr << endl;
}
#ifdef DEBUG
  #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
  #define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
  #define debug(...) 1337
  #define mdebug(a, n) 1337
#endif
template<typename T> ostream& operator << (ostream& stream, const vector<T>& v) {
  for (auto& x : v) {
    stream << x << ' ';
  }
  return stream;
}
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) {
  return stream << p.first << ' ' << p.second;
}


const ll inf64 = 1e18;

const int maxN = 1e6 + 6;

int n, c;
ll x[maxN], d[maxN];

ll val1[maxN];
int ord1[maxN], pos1[maxN];

ll val2[maxN];
int ord2[maxN], pos2[maxN];

/*int binsrchPref(ll x) {
  int vl = -1;
  int vr = n;
  while (vr - vl > 1) {
    int vm = (vl + vr) >> 1;
    if (val1[ord1[vm]] > x) {
      vl = vm;
    } else {
      vr = vm;
    }
  }
  return vl;
}

namespace fnw {
  ll v[2][maxN];

  void init() {
    fill(v[0], v[0] + n + 1, -inf64);
    fill(v[1], v[1] + n + 1, +inf64);
  }

  void enable(int p, ll x, ll y) {
    for (++p; p <= n; p += p & -p) {
      chkmax(v[0][p], x);
      chkmin(v[1][p], y);
    }
  }

  pll gt(int p) {
    pll ans = {-inf64, inf64};
    for (++p; p >= 1; p -= p & -p) {
      chkmax(ans.fi, v[0][p]);
      chkmin(ans.se, v[1][p]);
    }
    return ans;
  }
}*/

bool good(ll D) {
  ll slo = -inf64, shi = +inf64;
  ll dlo = -inf64, dhi = +inf64;

  /*fnw::init();
  for (int j = 1; j < n; ++j) {
    fnw::enable(pos1[j - 1], x[j - 1] + d[j - 1], x[j - 1] - d[j - 1]);
    int p = binsrchPref(D - x[j] - d[j]);
    auto v = fnw::gt(p);
    chkmax(slo, +v.fi + x[j] + d[j] - D + c); 
    chkmin(shi, +v.se + x[j] - d[j] + D - c);
    chkmax(dlo, -v.se + x[j] + d[j] - D + c);
    chkmin(dhi, -v.fi + x[j] - d[j] + D - c);
  }*/

  int p = 0;
  pll v = {-inf64, inf64};
  for (int ii = 0; ii < n; ++ii) {
    int j = ord2[ii];
    if (!j) continue;
    while (p < n && val1[p] > D - val2[j]) {
      int i = ord1[p];
      chkmax(v.fi, x[i] + d[i]);
      chkmin(v.se, x[i] - d[i]);
      ++p;
    }
    chkmax(slo, +v.fi + x[j] + d[j] - D + c); 
    chkmin(shi, +v.se + x[j] - d[j] + D - c);
    chkmax(dlo, -v.se + x[j] + d[j] - D + c);
    chkmin(dhi, -v.fi + x[j] - d[j] + D - c);
  }

  //debug(slo, shi, dlo, dhi);

  if (slo > shi || dlo > dhi) return false;

  for (int r = 1; r < n; ++r) {
    ll L = max(slo - x[r], x[r] - dhi);
    ll R = min(shi - x[r], x[r] - dlo);
    if (L <= R) {
      int l = lower_bound(x, x + n, L) - x;
      if (l < r && x[l] <= R) {
        return true;
      }
    }
  }
  return false;
}

ll find_shortcut(int _n, vector<int> dx, vector<int> _d, int _c) {
  n = _n;
  c = _c;
  for (int i = 0; i < n; ++i) d[i] = _d[i];
  for (int i = 0; i + 1 < n; ++i) {
    x[i + 1] = x[i] + dx[i];
  }

  int maxd1 = -1e9, maxd2 = -1e9;
  for (int i = 0; i < n; ++i) {
    if (d[i] >= maxd1) {
      maxd2 = maxd1;
      maxd1 = d[i];
    } else if (d[i] > maxd2) {
      maxd2 = d[i];
    }
  }

  for (int i = 0; i < n; ++i) {
    val1[i] = d[i] - x[i];
  }
  iota(ord1, ord1 + n, 0);
  sort(ord1, ord1 + n, [&](int i, int j) -> bool {
    return val1[i] > val1[j];
  });
  for (int i = 0; i < n; ++i) pos1[ord1[i]] = i;

  for (int i = 0; i < n; ++i) {
    val2[i] = d[i] + x[i];
  }
  iota(ord2, ord2 + n, 0);
  sort(ord2, ord2 + n, [&](int i, int j) -> bool {
    return val2[i] < val2[j];
  });
  for (int i = 0; i < n; ++i) pos2[ord2[i]] = i;

  ll vl = maxd1 + maxd2;
  ll vr = maxd1 + maxd2 + x[n - 1];
  while (vr - vl > 1) {
    ll vm = (vl + vr) >> 1;
    if (good(vm)) {
      vr = vm;
    } else {
      vl = vm;
    }
  }
  return vr;
}


#ifdef DEBUG
int32_t main() {
#ifdef DEBUG
  freopen("in", "r", stdin);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, c;
  cin >> n >> c;
  vector<int> l(n - 1), d(n);
  for (int& x : l) cin >> x;
  for (int& x : d) cin >> x;
  cout << find_shortcut(n, l, d, c) << '\n';

  return 0;
}
#endif

Compilation message

shortcut.cpp:1: warning: ignoring #pragma gcc optimize [-Wunknown-pragmas]
    1 | #pragma gcc optimize("Ofast")
      | 
shortcut.cpp:2: warning: ignoring #pragma gcc optimize [-Wunknown-pragmas]
    2 | #pragma gcc optimize("O3")
      | 
shortcut.cpp:3: warning: ignoring #pragma gcc optimize [-Wunknown-pragmas]
    3 | #pragma gcc optimize("no-stack-protector")
      | 
shortcut.cpp:4: warning: ignoring #pragma gcc optimize [-Wunknown-pragmas]
    4 | #pragma gcc optimize("unroll-loops")
      | 
shortcut.cpp:5: warning: ignoring #pragma gcc optimize [-Wunknown-pragmas]
    5 | #pragma gcc optimize("fast-math")
      | 
shortcut.cpp:6: warning: ignoring #pragma gcc target [-Wunknown-pragmas]
    6 | #pragma gcc target("avx,avx2,sse,sse2,sse3,ssse3,sse4,ffa")
      |
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB n = 4, 80 is a correct answer
2 Incorrect 1 ms 332 KB n = 9, incorrect answer: jury 110 vs contestant 100
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB n = 4, 80 is a correct answer
2 Incorrect 1 ms 332 KB n = 9, incorrect answer: jury 110 vs contestant 100
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB n = 4, 80 is a correct answer
2 Incorrect 1 ms 332 KB n = 9, incorrect answer: jury 110 vs contestant 100
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB n = 4, 80 is a correct answer
2 Incorrect 1 ms 332 KB n = 9, incorrect answer: jury 110 vs contestant 100
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB n = 4, 80 is a correct answer
2 Incorrect 1 ms 332 KB n = 9, incorrect answer: jury 110 vs contestant 100
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB n = 4, 80 is a correct answer
2 Incorrect 1 ms 332 KB n = 9, incorrect answer: jury 110 vs contestant 100
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB n = 4, 80 is a correct answer
2 Incorrect 1 ms 332 KB n = 9, incorrect answer: jury 110 vs contestant 100
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB n = 4, 80 is a correct answer
2 Incorrect 1 ms 332 KB n = 9, incorrect answer: jury 110 vs contestant 100
3 Halted 0 ms 0 KB -