Submission #222931

#TimeUsernameProblemLanguageResultExecution timeMemory
222931BruteforcemanSplit the sequence (APIO14_sequence)C++11
71 / 100
401 ms131076 KiB
#include <bits/stdc++.h> using namespace std; int a[100010]; int n, k; long long pre[100010]; long long dp[202][100010], opt[202][100010]; const long long inf = 1e17; void solve(int row, int l, int r, int start, int fin) { if(l > r) return ; int mid = (l + r) >> 1; dp[row][mid] = -inf; for(int i = start; i <= fin; i++) { if(i >= mid) break; long long cost = dp[row - 1][i] + (pre[n] - pre[mid] + pre[i]) * (pre[mid] - pre[i]); if(cost > dp[row][mid]) { dp[row][mid] = cost; opt[row][mid] = i; } } solve(row, l, mid - 1, start, opt[row][mid]); solve(row, mid + 1, r, opt[row][mid], fin); } int main() { scanf("%d %d", &n, &k); k += 1; for(int i = 1; i <= n; i++) { scanf("%d", a + i); } for(int i = 1; i <= n; i++) { pre[i] = pre[i - 1] + a[i]; } long long sum = pre[n]; for(int j = 1; j <= n; j++) { dp[0][j] = -inf; } dp[0][0] = 0; for(int i = 1; i <= k; i++) { dp[i][0] = -inf; solve(i, 1, n, 0, n - 1); } printf("%lld\n", dp[k][n] / 2); int cur = n; int cnt = k; while(cnt) { cur = opt[cnt--][cur]; if(cur) printf("%d ", cur); } puts(""); return 0; }

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:32:13: warning: unused variable 'sum' [-Wunused-variable]
   long long sum = pre[n];
             ^~~
sequence.cpp:24:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &n, &k);
   ~~~~~^~~~~~~~~~~~~~~~~
sequence.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     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...