제출 #320456

#제출 시각아이디문제언어결과실행 시간메모리
320456VROOM_VARUNShortcut (IOI16_shortcut)C++14
100 / 100
1738 ms62548 KiB
/*
ID: varunra2
LANG: C++
TASK: shortcut
*/

#include <bits/stdc++.h>
#include "shortcut.h"
using namespace std;

#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
  cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif

#define int long long

#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e16
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second

const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions

VI l;
VI x;
VI d;
VII use;
int n;
int c;

void cop(VI& a, vector<int32_t> b) {
  a.resize(sz(b));
  for (int i = 0; i < sz(a); i++) {
    a[i] = b[i];
  }
}

void genPref() {
  x.resize(n);
  x[0] = 0ll;
  for (int i = 1; i < n; i++) {
    x[i] = x[i - 1] + l[i - 1];
  }
}

int mnsum, mxsum, mndif, mxdif;

void upd(int i) {
  int x = ::x[i], d = ::d[i];
  int sum = x + d, dif = x - d;
  mnsum = min(mnsum, sum);
  mxsum = max(mxsum, sum);
  mndif = min(mndif, dif);
  mxdif = max(mxdif, dif);
}

void initsumdif() {
  mnsum = INF;
  mxsum = -INF;
  mndif = INF;
  mxdif = -INF;
}

int K;

bool optimize(int i, int j) {
  // do we need to optimize pair i and j?
  return x[j] + d[j] - (x[i] - d[i]) > K;
}

bool works(int k) {
  K = k;
  VI vals = {INF, -INF, INF, -INF};
  initsumdif();

  int p1 = 0, p2 = 0;

  for (int i = 0; i < n; i++) {
    while (p1 < p2 and optimize(use[p1].y, i)) {
      upd(use[p1++].y);
    }
    int sum = x[i] + d[i], dif = x[i] - d[i];
    vals[0] = min(vals[0], (k - c) + dif + mndif);
    vals[1] = max(vals[1], sum + mxsum - (k - c));
    vals[2] = min(vals[2], (k - c) + dif - mxsum);
    vals[3] = max(vals[3], sum - mndif - (k - c));

    while (p2 > p1 and use[p2 - 1].x > x[i] - d[i]) p2--;
    use[p2++] = MP(x[i] - d[i], i);
  }

  // now we need to find two points in the rectangle defined by vals
  int maxsum, minsum, maxdif, mindif;
  maxsum = vals[0];
  minsum = vals[1];
  maxdif = vals[2];
  mindif = vals[3];
  int curdif = 0;
  int cursum = n;
  if (maxsum < minsum or maxdif < mindif) return 0;
  for (int i = 0; i < n; i++) {
    while (curdif < n and x[curdif] - x[i] < mindif) curdif++;
    while (cursum > 0 and x[cursum - 1] + x[i] >= minsum) cursum--;
    int cur = max({cursum, curdif, i + 1});
    if (cur < n and x[cur] + x[i] <= maxsum and x[cur] - x[i] <= maxdif)
      return true;
  }
  return false;
}

ll find_shortcut(int32_t _n, vector<int32_t> _l, vector<int32_t> _d,
                 int32_t _c) {
  n = _n;
  c = _c;
  cop(l, _l);
  cop(d, _d);
  genPref();
  use.assign(n, MP(0, 0));
  int xx = -1;
  int z = INF;
  for (int b = z; b >= 1; b /= 2) {
    while (!works(xx + b)) xx += b;
  }
  return xx + 1;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...