제출 #392636

#제출 시각아이디문제언어결과실행 시간메모리
392636usachevd0Shortcut (IOI16_shortcut)C++14
38 / 100
2091 ms141632 KiB
#include <bits/stdc++.h> #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 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; ll find_shortcut(int n, vector<int> dx, vector<int> d, int c) { vector<ll> x(n); for (int i = 0; i + 1 < n; ++i) { x[i + 1] = x[i] + dx[i]; } vector<ll> prf_p(n, -inf64); for (int i = 1; i < n; ++i) { prf_p[i] = prf_p[i - 1]; for (int j = i - 1; j >= 0; --j) { chkmax(prf_p[i], d[j] + x[i] - x[j] + d[i]); } } vector<ll> suf_p(n + 1, -inf64); for (int i = n - 2; i >= 0; --i) { suf_p[i] = suf_p[i + 1]; for (int j = i + 1; j < n; ++j) { chkmax(suf_p[i], d[i] + x[j] - x[i] + d[j]); } } vector<ll> v[2]; vector<vector<ll>> maxv[2]; for (int t = 0; t < 2; ++t) { v[t].resize(n); for (int i = 0; i < n; ++i) { if (t == 0) { v[t][i] = x[i] + d[i]; } else { v[t][i] = -x[i] + d[i]; } } maxv[t].assign(n, vector<ll>(n, -inf64)); for (int l = 0; l < n; ++l) { maxv[t][l][l] = v[t][l]; for (int r = l + 1; r < n; ++r) { maxv[t][l][r] = max(maxv[t][l][r - 1], v[t][r]); } } } vector<ll> prf_v(n + 1, -inf64); for (int i = 0; i < n; ++i) { chkmax(prf_v[i], -x[i] + d[i]); chkmax(prf_v[i + 1], prf_v[i]); } vector<ll> suf_v(n + 1, -inf64); for (int i = n - 1; i >= 0; --i) { suf_v[i] = max(suf_v[i + 1], x[i] + d[i]); } ll ans = prf_p[n - 1]; for (int l = 0; l < n - 1; ++l) { for (int r = l + 1; r < n; ++r) { if (x[r] - x[l] <= c) continue; ll cur = -inf64; ll P = x[r] - x[l] + c; chkmax(cur, prf_p[l]); chkmax(cur, suf_p[r]); chkmax(cur, prf_v[l] + x[l] + c + suf_v[r] - x[r]); for (int i = l + 1; i < r; ++i) { chkmax(cur, d[i] + min(x[i] - x[l] + c, x[r] - x[i]) + suf_v[r] - x[r]); } for (int j = l + 1; j < r; ++j) { chkmax(cur, d[j] + min(x[r] - x[j] + c, x[j] - x[l]) + prf_v[l] + x[l]); } int j = l + 1; for (int i = l + 1; i + 1 < r; ++i) { chkmax(j, i); while (j + 1 < r && 2 * (x[j + 1] - x[i]) <= P) { ++j; } chkmax(cur, d[i] + maxv[0][i][j] - x[i]); chkmax(cur, d[i] + maxv[1][j + 1][r - 1] + x[i] + P); /*for (int j = i + 1; j < r; ++j) { chkmax(cur, d[i] + d[j] + min(x[j] - x[i], x[i] - x[l] + c + x[r] - x[j])); }*/ } chkmin(ans, cur); } } return ans; } #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
#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...