Advertisement
erek1e

IOI '00 P5 - Post Office (2)

Jun 8th, 2022
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int INF = 1e9;
  6.  
  7. int main() {
  8.     int n, m;
  9.     cin >> n >> m;
  10.     vector<int> x(1+n);
  11.     for (int i = 1; i <= n; ++i) cin >> x[i], x[i] += x[i-1];
  12.    
  13.     auto A = [&x](int l, int r) {
  14.         return (x[r]-x[r-(r-l+1)/2]) - (x[l+(r-l+1)/2-1]-x[l-1]);
  15.     };
  16.  
  17.     vector<vector<int>> dp(1+n, vector<int>(1+n, INF));
  18.     vector<vector<int>> pos(1+n, vector<int>(2+n));
  19.     for (int i = 1; i <= n; ++i) {
  20.         dp[i][1] = A(1, i);
  21.         pos[i][i+1] = i-1;
  22.     }
  23.     for (int i = 2; i <= n; ++i) {
  24.         for (int j = i; j >= 2; --j) {
  25.             for (int k = pos[i-1][j]; k <= pos[i][j+1]; ++k) {
  26.                 dp[i][j] = min(dp[i][j], dp[k][j-1] + A(k+1, i));
  27.             }
  28.             for (int k = pos[i-1][j]; k <= pos[i][j+1]; ++k) {
  29.                 if (dp[i][j] == dp[k][j-1] + A(k+1, i)) pos[i][j] = k;
  30.             }
  31.         }
  32.     }
  33.     cout << dp[n][m] << endl;
  34.     vector<int> v;
  35.     for (int i = n, j = m; j >= 1; i = pos[i][j--]) {
  36.         int k = (i + pos[i][j]+1)/2;
  37.         v.push_back(x[k]-x[k-1]);
  38.     }
  39.     for (int i = m-1; i >= 0; --i) cout << v[i] << ' ';
  40.     cout << endl;
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement