Submission #764550

#TimeUsernameProblemLanguageResultExecution timeMemory
764550marvinthangShortcut (IOI16_shortcut)C++17
100 / 100
1298 ms86208 KiB
/************************************* * author: marvinthang * * created: 23.06.2023 20:12:10 * *************************************/ #include "shortcut.h" #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define left ___left #define right ___right #define TIME (1.0 * clock() / CLOCKS_PER_SEC) #define MASK(i) (1LL << (i)) #define BIT(x, i) ((x) >> (i) & 1) #define __builtin_popcount __builtin_popcountll #define ALL(v) (v).begin(), (v).end() #define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i) #define REPD(i, n) for (int i = (n); i--; ) #define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i) #define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; ) #define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i) #define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u) #define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u) #ifdef LOCAL #include "debug.h" #else #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } #define DB(...) 23 #define db(...) 23 #define debug(...) 23 #endif template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; } template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; } template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; } template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); } template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); } template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; } template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; } template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; } // end of template const int MAX = 1e6 + 6; const long long INF = 1e9 * MAX; void up_max(pair <long long, long long> &a, long long b) { if (a.fi < b) { a.se = a.fi; a.fi = b; } else maximize(a.se, b); } void up_min(pair <long long, long long> &a, long long b) { if (a.fi > b) { a.se = a.fi; a.fi = b; } else minimize(a.se, b); } long long find_shortcut(int n, vector <int> l, vector <int> d, int c) { vector <long long> x(n); REP(i, n - 1) x[i + 1] = x[i] + l[i]; vector <long long> v_d; REP(i, n) v_d.push_back(x[i] - d[i]); sort(ALL(v_d)); v_d.erase(unique(ALL(v_d)), v_d.end()); int m = v_d.size(); vector <pair <long long, long long>> max_s(m, make_pair(-INF, -INF)), min_d(m, make_pair(INF, INF)); REP(i, n) { int p = lower_bound(ALL(v_d), x[i] - d[i]) - v_d.begin(); up_min(min_d[p], x[i] - d[i]); up_max(max_s[p], x[i] + d[i]); } REP(i, m - 1) { up_min(min_d[i + 1], min_d[i].fi); up_min(min_d[i + 1], min_d[i].se); up_max(max_s[i + 1], max_s[i].fi); up_max(max_s[i + 1], max_s[i].se); } vector <int> p_s(n); iota(ALL(p_s), 0); sort(ALL(p_s), [&] (int a, int b) { return x[a] + d[a] < x[b] + d[b]; }); auto check = [&] (long long f) { long long max_diff = INF, min_diff = -INF, max_sum = INF, min_sum = -INF; int l = -1; for (int r: p_s) { while (l + 1 < m && v_d[l + 1] < x[r] + d[r] - f - c) ++l; if (l >= 0) { long long a = max_s[l].fi == x[r] + d[r] ? max_s[l].se : max_s[l].fi; long long b = min_d[l].fi == x[r] - d[r] ? min_d[l].se : min_d[l].fi; minimize(max_diff, (x[r] - d[r]) - a + f); maximize(min_diff, (x[r] + d[r]) - b - f); minimize(max_sum, (x[r] - d[r]) + b + f); maximize(min_sum, (x[r] + d[r]) + a - f); } } auto check = [&] (int i, int j) { return min_diff <= x[i] - x[j] && x[i] - x[j] <= max_diff && min_sum <= x[i] + x[j] && x[i] + x[j] <= max_sum; }; int j = 0, k = n - 1; REP(i, n) { while (j + 1 < n && x[j] < x[i] - max_diff) ++j; if (check(i, j)) return true; while (k && x[k - 1] >= min_sum - x[i]) --k; if (check(i, j) || check(i, k)) return true; } return false; }; long long ll = *max_element(ALL(d)); long long rr = x[n - 1] + 2 * ll; while (ll <= rr) { long long m = ll + rr >> 1; if (check(m - c)) rr = m - 1; else ll = m + 1; } return ll; } #ifdef LOCAL #include <cstdio> #include <cassert> #include "shortcut.h" int main() { file("shortcut"); int n, c; assert(2 == scanf("%d%d", &n, &c)); std::vector<int> l(n - 1); std::vector<int> d(n); for (int i = 0; i < n - 1; i++) assert(1 == scanf("%d", &l[i])); for (int i = 0; i < n; i++) assert(1 == scanf("%d", &d[i])); long long t = find_shortcut(n, l, d, c); printf("%lld\n", t); return 0; } #endif

Compilation message (stderr)

shortcut.cpp: In function 'long long int find_shortcut(int, std::vector<int>, std::vector<int>, int)':
shortcut.cpp:118:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  118 |         long long m = ll + rr >> 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...