Submission #41955

#TimeUsernameProblemLanguageResultExecution timeMemory
41955festSplit the sequence (APIO14_sequence)C++14
71 / 100
322 ms24752 KiB
// fest #include <bits/stdc++.h> #define pb push_back #define F first #define S second #define y1 dasdasfasfas #define x1 wqdadfasfasfas #define All(c) c.begin(), c.end() #define SZ(A) (int((A).size())) #define umap unordered_map #define FILENAME "" #define __ fflush(stdout) typedef long long ll; typedef long double ld; using namespace std; void FREOPEN() { #ifdef COMP freopen(".in", "r", stdin); freopen("1.out", "w", stdout); #endif } inline double Time() {return (clock() * 1.0) / CLOCKS_PER_SEC; } const int N = 10001, M = 201, inf = 1e9 * 2, MOD = (int)1e9 + 7; char CH[N]; const double eps = 1e-14; const ll INF = 1e18; const int dx[] = {1, -1, 0, 0, -1, 1, -1, 1}; const int dy[] = {0, 0, 1, -1, -1, 1, 1, -1}; int pref[N], a[N], pr[N][M]; ll dp[N][M]; double meet(int a1, ll b1, int a2, ll b2) { return (double)(b2 - b1) / (double)(a1 - a2); } int main() { FREOPEN(); int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) scanf("%d ", a + i); for (int i = 1; i <= n; i++) pref[i] = pref[i - 1] + a[i]; for (int i = 0; i <= n; i++) for (int j = 0; j <= k; j++) dp[i][j] = -INF, pr[i][j] = -1; dp[0][0] = 0; vector<pair<int, pair<ll, int> > > lines; for (int bl = 1; bl <= k; bl++) { lines.clear(); lines.pb({-pref[bl - 1], {dp[bl - 1][bl - 1], bl - 1}}); for (int i = bl; i <= n; i++) { int x = pref[n] - pref[i]; int l = 0; int r = SZ(lines) - 1; while (r - l > 1) { int mid = (l + r) / 2; if (x * 1ll * lines[mid].F + lines[mid].S.F < x * 1ll * lines[mid + 1].F + lines[mid + 1].S.F) l = mid; else r = mid; } if (x * 1ll * lines[l].F + lines[l].S.F < x * 1ll * lines[r].F + lines[r].S.F) swap(l, r); dp[i][bl] = x * 1ll * pref[i] + x * 1ll * lines[l].F + lines[l].S.F; pr[i][bl] = lines[l].S.S; while (SZ(lines) >= 2 && meet(lines.back().F, lines.back().S.F, -pref[i], dp[i][bl - 1]) > meet(lines[SZ(lines) - 2].F, lines[SZ(lines) - 2].S.F, -pref[i], dp[i][bl - 1]) - eps) lines.pop_back(); while (!lines.empty() && lines.back().F == -pref[i] && lines.back().S.F == dp[i][bl - 1]) lines.pop_back(); lines.pb({-pref[i], {dp[i][bl - 1], i}}); } } int pos = 0; ll ans = -INF; for (int i = 1; i <= n; i++) { if (ans < dp[i][k]) { ans = dp[i][k]; pos = i; } } vector<int> out; cout << ans << endl; int bl = k; while (pos > 0) { out.pb(pos); pos = pr[pos][bl]; bl--; } sort(All(out)); for (auto i : out) printf("%d ", i); return 0; }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:53:50: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i = 1; i <= n; i++) scanf("%d ", a + i);
                                                  ^
#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...