Submission #503124

#TimeUsernameProblemLanguageResultExecution timeMemory
503124stanislavpolynBigger segments (IZhO19_segments)C++17
37 / 100
6 ms460 KiB
#include <bits/stdc++.h> #define fr(i, a, b) for(int i = (a); i <= (b); ++i) #define rf(i, a, b) for(int i = (a); i >= (b); --i) #define fe(x, y) for(auto& x : y) #define fi first #define se second #define pb push_back #define mp make_pair #define mt make_tuple #define all(x) (x).begin(), (x).end() #define pw(x) (1LL << (x)) #define sz(x) (int)(x).size() using namespace std; mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); #include <ext/pb_ds/assoc_container.hpp> using namespace __gnu_pbds; template<typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define fbo find_by_order #define ook order_of_key template<typename T> bool umn(T& a, T b) { return (a > b ? (a = b, 1) : 0); } template<typename T> bool umx(T& a, T b) { return (a < b ? (a = b, 1) : 0); } using ll = long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; template<typename T> using ve = vector<T>; const int N = 3e3 + 5; int n; int a[N]; ll pref[N]; int dp[N][N]; pii par[N][N]; int brute1() { fr(i, 0, n) { fr(j, 0, n) { dp[i][j] = -1e9; } } fr(i, 1, n) { dp[i][1] = 1; rf(j, i, 1) { if(dp[i][j] != -1e9) { fr(nxt, i + 1, n) { if(pref[nxt] - pref[i] >= pref[i] - pref[j - 1]) { if(umx(dp[nxt][i + 1], dp[i][j] + 1)) { par[nxt][i + 1] = {i, j}; } } } break; } } } int mx = 0; pii best; fr(i, 1, n) { fr(j, 1, n) { if(umx(mx, dp[i][j])) { best = {i, j}; } } } ve<int> ans; while(1) { ans.pb(best.fi); if(best.se == 1) break; best = par[best.fi][best.se]; } reverse(all(ans)); // cout << "ans: "; // fe(x, ans) cout << x << " "; // cout << "\n"; int prev = 0; fe(x, ans) { // cout << pref[x] - pref[prev] << " "; prev = x; } // cout << "\n"; return mx; } int solve() { ve<int> dp(n + 1), cnt(n + 1); fr(i, 0, n) dp[i] = -1e9, cnt[i] = -1e9; fr(i, 1, n) { if(umx(dp[i], 1)) { cnt[i] = 1; } if(umx(dp[i + 1], dp[i])) { cnt[i + 1] = cnt[i]; } ll sum = 0; fr(j, i + 1, n) { sum += a[j]; if(sum >= pref[i] - pref[dp[i] - 1]) { if(umx(dp[j], i + 1)) { cnt[j] = cnt[i] + 1; } break; } } } return *max_element(all(cnt)); } int main() { #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); ios::sync_with_stdio(0); cin.tie(0); #else // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); ios::sync_with_stdio(0); cin.tie(0); #endif cin >> n; fr(i, 1, n) { cin >> a[i]; // a[i] = rng() % int(1e6) + 1; pref[i] = a[i] + pref[i - 1]; } // fr(i, 1, n) cout << a[i] << " "; // cout << "\n"; // cout << brute1() << "\n"; // cout << brute2() << "\n"; cout << solve() << "\n"; return 0; }

Compilation message (stderr)

segments.cpp: In function 'int brute1()':
segments.cpp:92:9: warning: variable 'prev' set but not used [-Wunused-but-set-variable]
   92 |     int prev = 0;
      |         ^~~~
#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...