Submission #634112

#TimeUsernameProblemLanguageResultExecution timeMemory
634112SOCIOPATESplit the sequence (APIO14_sequence)C++17
50 / 100
2089 ms32856 KiB
#ifdef LOCAL #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; typedef unsigned long long ull; #define pll pair<ll, ll> #define pii pair<int, int> #define pdd pair<ld, ld> #define ff first #define ss second #define all(v) v.begin(),v.end() typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordset; #pragma GCC optimize("-O3") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("-Os") ll INF = 2e18; ll mod = 1e9 + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ll binpow(ll n, ll m){ if(m == 0) return 1ll; if(m & 1ll) return n * binpow(n, m / 2ll); ll b = binpow(n, m / 2ll); return b*b; } void solve(){ int n, k; cin >> n >> k; vector<ll> a(n), pref(n); for(int i = 0; i < n; i++) { cin >> a[i]; pref[i] = (i ? pref[i - 1] : 0) + a[i]; } vector<vector<ll>> dp(n, vector<ll>(k + 2, -INF)), pr(n, vector<ll>(k + 2, -1)); auto sum = [&](int l, int r){ return pref[r] - (l ? pref[l - 1] : 0); }; for(int i = 0; i < n; i++) dp[i][1] = pref[i]*(pref[n - 1]-pref[i]); for(int i = 1; i < n; i++){ for(int j = 2; j <= min(i + 1, k + 1); j++){ for(int i1 = j - 2; i1 < i; i1++){ ll newdp; if(i == n - 1 && j == k + 1) newdp = dp[i1][j - 1]; else newdp = sum(i1 + 1, i)*sum(i + 1, n - 1) + dp[i1][j - 1]; if(dp[i][j] < newdp){ pr[i][j] = i1; dp[i][j] = newdp; } } } } vector<int> ans; int lst = n - 1; for(int i = k + 1; i >= 2; i--){ lst = pr[lst][i]; ans.push_back(lst); } cout << dp[n - 1][k + 1] << '\n'; reverse(all(ans)); for(int u : ans) cout << u + 1 << ' '; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); #ifdef LOCAL freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); #endif int tt; //cin >> tt; tt = 1; while (tt--) { solve(); //solve(); #ifdef LOCAL cout << "__________________________________" << endl; #endif } #ifdef LOCAL cout << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << "sec" << '\n'; #endif return 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...
#Verdict Execution timeMemoryGrader output
Fetching results...