Submission #173470

#TimeUsernameProblemLanguageResultExecution timeMemory
173470emil_physmathBigger segments (IZhO19_segments)C++17
100 / 100
125 ms18680 KiB
#include <algorithm> #include <iostream> #include <vector> using namespace std; typedef double ldouble; typedef long long llong; typedef unsigned int uint; typedef pair<int, llong> Dp; template<typename T1, typename T2> inline ostream& operator<<(ostream& str, const pair<T1, T2>& p) { return str << "{" << p.first << ", " << p.second << "}"; } inline const Dp& BestDp(const Dp& a, const Dp& b) { if (a.first == b.first) return a.second < b.second ? a : b; return a.first > b.first ? a : b; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin >> n; vector<int> a(n); for (int& x: a) cin >> x; vector<llong> pref(n); for (int i = 0; i < n; ++i) pref[i] = a[i] + (i ? pref[i - 1] : 0); vector<Dp> dp(n); for (int i = 0; i < n; ++i) dp[i] = {1, pref[i]}; for (int i = 0; i + 1 < n; ++i) { dp[i + 1] = BestDp(dp[i + 1], {dp[i].first, dp[i].second + a[i + 1]}); int j = -1; int l = i + 1, r = n - 1; while (l <= r) { int m = (l + r) / 2; if (pref[m] - pref[i] >= dp[i].second) { j = m; r = m - 1; } else l = m + 1; } if (j != -1) { dp[j] = BestDp(dp[j], {dp[i].first + 1, pref[j] - pref[i]}); // cerr << "i: " << i << "; " << "j: " << j << "\t" << dp[j] << endl; } } /*for (int i = 0; i < dp.size(); ++i) cerr << "dp[" << i << "] = " << dp[i] << endl;*/ cout << max_element(dp.begin(), dp.end())->first << endl; }
#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...