Submission #640937

#TimeUsernameProblemLanguageResultExecution timeMemory
640937piOOEShortcut (IOI16_shortcut)C++17
38 / 100
2085 ms40364 KiB
#include <bits/stdc++.h>
#include "shortcut.h"

using namespace std;
using ll = long long;

ll find_shortcut(int n, vector<int> l, vector<int> d, int c) {
    vector<ll> pref(n);
    for (int i = 0; i < n - 1; ++i) {
        pref[i + 1] = pref[i] + l[i];
    }

    auto dist = [&](int i, int j) -> long long {
        if (i > j) swap(i, j);
        return pref[j] - pref[i];
    };

    ll low = 0, high = pref[n - 1] + (*max_element(d.begin(), d.end())) * 2;
    while (low + 1 < high) {
        ll mid = (low + high) >> 1;
        auto check = [&]() -> bool {
            vector<vector<int>> cant(n), mx_pref(n), mx_suf(n), nxt(n);
            vector<int> prefix;
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; ++j) {
                    if (i != j && dist(i, j) + d[i] + d[j] > mid) {
                        cant[i].push_back(j);
                    }
                }
                if (!cant[i].empty()) {
                    if (cant[i][0] < i && cant[i].back() > i) {
                        return false;
                    }
                    if (cant[i][0] > i) {
                        nxt[i].assign(n + 1, cant[i].size());
                        for (int j = 0; j < cant[i].size(); ++j) {
                            nxt[i][cant[i][j]] = j;
                        }
                        for (int j = n - 1; j > -1; --j) {
                            if (nxt[i][j] == cant[i].size()) {
                                nxt[i][j] = nxt[i][j + 1];
                            }
                        }
                        prefix.push_back(i);
                        mx_pref[i].resize(cant[i].size());
                        mx_suf[i].resize(cant[i].size());
                        for (int j = 0; j < cant[i].size(); ++j) {
                            if (j == 0 || d[cant[i][j]] > dist(cant[i][j], mx_pref[i][j - 1]) + d[mx_pref[i][j - 1]]) {
                                mx_pref[i][j] = cant[i][j];
                            } else {
                                mx_pref[i][j] = mx_pref[i][j - 1];
                            }
                        }
                        for (int j = cant[i].size() - 1; j > -1; --j) {
                            if (j == size(cant[i]) - 1 ||
                                d[cant[i][j]] > dist(cant[i][j], mx_suf[i][j + 1]) + d[mx_suf[i][j + 1]]) {
                                mx_suf[i][j] = cant[i][j];
                            } else {
                                mx_suf[i][j] = mx_suf[i][j + 1];
                            }
                        }
                    }
                }
            }
            for (int p = 0; p < n; ++p) {
                int L = 0, R = n;
                for (int i: prefix) {
                    ll D = dist(p, i) + d[i];
                    int l = -1, r = n;
                    while (l + 1 < r) {
                        int m = (l + r) >> 1;
                        auto it = nxt[i][m];
                        if (it == cant[i].size() || D + c + dist(m, mx_suf[i][it]) + d[mx_suf[i][it]] <= mid) {
                            r = m;
                        } else {
                            l = m;
                        }
                    }
                    L = max(L, r);
                    l = -1, r = n;
                    while (l + 1 < r) {
                        int m = (l + r) >> 1;
                        auto it = nxt[i][m + 1];
                        if (it == 0 || D + c + dist(m, mx_pref[i][it - 1]) + d[mx_pref[i][it - 1]] <= mid) {
                            l = m;
                        } else {
                            r = m;
                        }
                    }
                    R = min(R, r);
                    if (L >= R) {
                        break;
                    }
                }
                if (L < R) {
                    return true;
                }
            }
            return false;
        };
        if (check()) {
            high = mid;
        } else {
            low = mid;
        }
    }
    return high;
}

Compilation message (stderr)

shortcut.cpp: In lambda function:
shortcut.cpp:36:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |                         for (int j = 0; j < cant[i].size(); ++j) {
      |                                         ~~^~~~~~~~~~~~~~~~
shortcut.cpp:40:43: warning: comparison of integer expressions of different signedness: '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |                             if (nxt[i][j] == cant[i].size()) {
shortcut.cpp:47:43: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |                         for (int j = 0; j < cant[i].size(); ++j) {
      |                                         ~~^~~~~~~~~~~~~~~~
shortcut.cpp:55:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |                             if (j == size(cant[i]) - 1 ||
      |                                 ~~^~~~~~~~~~~~~~~~~~~~
shortcut.cpp:73:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   73 |                         if (it == cant[i].size() || D + c + dist(m, mx_suf[i][it]) + d[mx_suf[i][it]] <= mid) {
      |                             ~~~^~~~~~~~~~~~~~~~~
#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...