#if defined(EMBE_DEBUG) && !defined(NDEBUG) #include "embe-debug.hpp" #else #define LOG_INDENT(...) do {} while (false) #define LOG(...) do {} while (false) #define DUMP(...) do {} while (false) #endif #include <iostream> #include <map> #include <optional> #include <vector> using namespace std; namespace { class State { private: map<long long, int> current; long long energy_bias = 0; int cost_bias = 0; public: void insert(long long e, int c) { e -= energy_bias; c -= cost_bias; auto it = current.lower_bound(e); if (it != current.end() && it->second <= c) return; it = current.insert_or_assign(it, e, c); while (it != current.begin()) { auto it2 = prev(it); if (it2->second < c) break; current.erase(it2); } } void add_energy(long long e) { energy_bias += e; } void add_cost(int c) { cost_bias += c; } optional<int> find(long long e) { e -= energy_bias; auto it = current.lower_bound(e); if (it == current.end()) return {}; return it->second + cost_bias; } }; int solve(vector<int> const& cities) { State state; state.insert(0, 0); for (auto const& c: cities) { state.add_energy(c); auto ending = state.find(0); state.add_cost(1); if (ending) { state.insert(0, *ending); } } return state.find(0).value_or(-1); } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> cities(n); for (auto& c: cities) { cin >> c; } cout << solve(cities) << '\n'; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | #if defined(EMBE_DEBUG) && !defined(NDEBUG) #include "embe-debug.hpp" #else #define LOG_INDENT(...) do {} while (false) #define LOG(...) do {} while (false) #define DUMP(...) do {} while (false) #endif #include <iostream> #include <map> #include <optional> #include <vector> using namespace std; namespace { class State { private: map<long long, int> current; long long energy_bias = 0; int cost_bias = 0; public: void insert(long long e, int c) { e -= energy_bias; c -= cost_bias; auto it = current.lower_bound(e); if (it != current.end() && it->second <= c) return; it = current.insert_or_assign(it, e, c); while (it != current.begin()) { auto it2 = prev(it); if (it2->second < c) break; current.erase(it2); } } void add_energy(long long e) { energy_bias += e; } void add_cost(int c) { cost_bias += c; } optional<int> find(long long e) { e -= energy_bias; auto it = current.lower_bound(e); if (it == current.end()) return {}; return it->second + cost_bias; } }; int solve(vector<int> const& cities) { State state; state.insert(0, 0); for (auto const& c: cities) { state.add_energy(c); auto ending = state.find(0); state.add_cost(1); if (ending) { state.insert(0, *ending); } } return state.find(0).value_or(-1); } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> cities(n); for (auto& c: cities) { cin >> c; } cout << solve(cities) << '\n'; return 0; } |