Submission #768034

#TimeUsernameProblemLanguageResultExecution timeMemory
768034SanguineChameleonShortcut (IOI16_shortcut)C++17
100 / 100
1389 ms57020 KiB
#include "shortcut.h" #include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 20; const long long inf = 1e15L + 20; long long pos[maxn]; int depth[maxn]; int order1[maxn]; int order2[maxn]; int n, c; bool cmp1(int i, int j) { return pos[i] + depth[i] < pos[j] + depth[j]; } bool cmp2(int i, int j) { return pos[i] - depth[i] < pos[j] - depth[j]; } bool check(long long lim) { long long sum_lt = -inf; long long sum_rt = inf; long long diff_lt = -inf; long long diff_rt = inf; int max1 = -1; int max2 = -1; int pt = 0; for (int i = 1; i <= n; i++) { while (pt < n && pos[order2[pt + 1]] - depth[order2[pt + 1]] < pos[order1[i]] + depth[order1[i]] - lim) { pt++; if (max1 == -1) { max1 = order2[pt]; } else if (pos[order2[pt]] + depth[order2[pt]] > pos[max1] + depth[max1]) { max2 = max1; max1 = order2[pt]; } else if (max2 == -1) { max2 = order2[pt]; } else if (pos[order2[pt]] + depth[order2[pt]] > pos[max2] + depth[max2]) { max2 = order2[pt]; } } if (pt >= 1 && order2[1] != order1[i]) { sum_rt = min(sum_rt, (pos[order1[i]] - depth[order1[i]]) + (pos[order2[1]] - depth[order2[1]]) + (lim - c)); diff_rt = min(diff_rt, (-pos[order1[i]] - depth[order1[i]]) + (pos[order2[1]] - depth[order2[1]]) + (lim - c)); } else if (pt >= 2) { sum_rt = min(sum_rt, (pos[order1[i]] - depth[order1[i]]) + (pos[order2[2]] - depth[order2[2]]) + (lim - c)); diff_rt = min(diff_rt, (-pos[order1[i]] - depth[order1[i]]) + (pos[order2[2]] - depth[order2[2]]) + (lim - c)); } if (max1 != -1 && max1 != order1[i]) { sum_lt = max(sum_lt, (pos[order1[i]] + depth[order1[i]]) + (pos[max1] + depth[max1]) - (lim - c)); diff_lt = max(diff_lt, (-pos[order1[i]] + depth[order1[i]] + (pos[max1] + depth[max1]) - (lim - c))); } else if (max2 != -1) { sum_lt = max(sum_lt, (pos[order1[i]] + depth[order1[i]]) + (pos[max2] + depth[max2]) - (lim - c)); diff_lt = max(diff_lt, (-pos[order1[i]] + depth[order1[i]] + (pos[max2] + depth[max2]) - (lim - c))); } } swap(diff_lt, diff_rt); diff_lt = -diff_lt; diff_rt = -diff_rt; if (sum_lt > sum_rt || diff_lt > diff_rt) { return false; } int lt1 = n; int lt2 = 1; for (int i = 1; i <= n; i++) { while (lt1 > 1 && pos[i] + pos[lt1 - 1] >= sum_lt) { lt1--; } while (lt2 <= n && pos[lt2] < pos[i] - diff_rt) { lt2++; } int lt = max(lt1, lt2); if (lt < i && pos[lt] <= sum_rt - pos[i] && pos[lt] <= pos[i] - diff_lt) { return true; } } return false; } long long find_shortcut(int _n, vector<int> len, vector<int> _depth, int _c) { n = _n; c = _c; pos[1] = 0; for (int i = 2; i <= n; i++) { pos[i] = pos[i - 1] + len[i - 2]; } for (int i = 1; i <= n; i++) { order1[i] = i; order2[i] = i; depth[i] = _depth[i - 1]; } sort(order1 + 1, order1 + n + 1, cmp1); sort(order2 + 1, order2 + n + 1, cmp2); long long lt = 0; long long rt = inf; long long res = -1; while (lt <= rt) { long long mt = (lt + rt) / 2; if (check(mt)) { res = mt; rt = mt - 1; } else { lt = mt + 1; } } return res; }
#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...