Submission #855586

#TimeUsernameProblemLanguageResultExecution timeMemory
855586Nuraly_SerikbaySplit the sequence (APIO14_sequence)C++17
60 / 100
65 ms41052 KiB
//çalışan kazanır// #include "bits/stdc++.h" using namespace std; #define int long long #define pb push_back #define S second #define F first #define all(x) x.begin(),x.end() #define YOSIK() ios_base::sync_with_stdio(0),cin.tie(0) int gcd (int a, int b) {if (b == 0){return a;}else {return gcd (b, a % b);}} const int N = 2e5 + 1; const int INF = 1e18 + 1; const int MOD = 998244353ll; const int MODD = 1070234587ll; const double eps = 0.0001; //const int p = 87; int n, dp[10005][205], a[N], pref[N], suf[N]; int k; void solution () { cin >> n >> k; for (int i = 1; i <= n; ++ i) cin >> a[i]; for (int i = 1; i <= n; ++ i) pref[i] = a[i] + pref[i - 1]; for (int i = n; i >= 1; -- i) suf[i] = suf[i + 1] + a[i]; for (int t = 1; t <= k; ++ t) { for (int i = 1; i <= n; ++ i) dp[i][t] = -INF; } for (int i = 1; i < n; ++ i) dp[i][1] = pref[i] * suf[i + 1]; vector <pair <int, int>> ord; for (int t = 2; t <= k; ++ t) { ord.clear(); ord.pb ({INF, 0}); for (int i = 1; i < n; ++ i) { int l = 0, r = ord.size() - 1, rs = 0; while (l <= r) { int mid = (l + r) >> 1; if (ord[mid].F >= suf[i + 1]) l = mid + 1, rs = mid; else r = mid - 1; } rs = ord[rs].S; dp[i][t] = dp[rs][t - 1] + (pref[i] - pref[rs]) * suf[i + 1]; if (dp[i][t - 1] == -INF) continue; int per = INF; while (ord.size()) { if (pref[ord.back().S] == pref[i]) {; if (dp[i][t - 1] >= dp[ord.back().S][t - 1]) ord.pop_back(); else break; continue; } per = (dp[i][t - 1] - dp[ord.back().S][t - 1]) / (-pref[ord.back().S] + pref[i]); if (per > ord.back().F) ord.pop_back(); else break; } ord.pb ({per, i}); } } int res = 0, bg = 0; for (int i = 1; i <= n; ++ i) { res = max (res, dp[i][k]); if (dp[i][k] == res) bg = i; } cout << res << '\n'; vector <int> ans; ans.pb (bg); for (int t = k - 1; t >= 1; -- t) { for (int i = 1; i < bg; ++ i) { if (dp[i][t] + (pref[bg] - pref[i]) * suf[bg + 1] == res) { bg = i; ans.pb (i); res = dp[i][t]; break; } } } reverse (all (ans)); for (auto i: ans) cout << i << ' '; cout << '\n'; return; } signed main() { YOSIK(); // srand (time(0)); // freopen ("E.in", "r", stdin); // freopen ("E.out", "w", stdout); int TT = 1;// cin >> TT; for (int i = 1; i <= TT; ++ i) { // cout << "Case " << i << ": "; solution (); } 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...