Guest User

Untitled

a guest
Sep 15th, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define all(x) x.begin(), x.end()
  6. #define inf (long long) 1e18
  7.  
  8. typedef long long ll;
  9. typedef long double ld;
  10.  
  11. const int N = 123456;
  12. const int M = 1234567;
  13. const int P = 400004;
  14. const int A = 256;
  15.  
  16. const int n = 257;
  17.  
  18. int a[n + 2];
  19. long long bstInBetween[n + 2][n + 2];
  20. long long dp[n + 2][n + 2];
  21.  
  22. int main(int argc, char const *argv[])
  23. {
  24.    
  25.     #ifdef Cyborg1o1
  26.         freopen("input.txt", "r", stdin);
  27.         freopen("output.txt", "w", stdout);
  28.     #endif  
  29.  
  30.     cin.sync_with_stdio(0);
  31.     cin.tie(0);
  32.     cout.tie(0);
  33.  
  34.  
  35.     int d, k;
  36.     cin >> d >> k;
  37.     k++;
  38.  
  39.     int mx = 0;
  40.     for(int i = 0; i < d; ++i){
  41.         int x, y;
  42.         cin >> x >> y;
  43.         a[x + 1] = y;
  44.         mx = max(mx, x + 1);
  45.     }
  46.  
  47.     for(int i = 0; i < n; ++i){ //2nd to last v
  48.         for(int j = i + 1; j <= n; ++j){ //last v
  49.             for(int kk = i; kk <= j; ++kk){
  50.                 int dist = min(i == 0? n : kk - i, j >= mx + 1 ? n : j - kk);
  51.                 bstInBetween[i][j] += a[kk] * 1ll * dist * dist;
  52.             }
  53.             // cout << "i " << i << " j " << j << " " << bstInBetween[i][j] << endl;
  54.         }
  55.     }
  56.  
  57.     memset(dp, 0x7f, sizeof dp);
  58.  
  59.     dp[0][0] = 0;
  60.  
  61.     for(int i = 1; i <= mx + 1; ++i){ //possible values for vi
  62.         for(int j = 1; j <= k && j <= i; ++j){ //cluster number
  63.             for(int kk = i - 1; kk >= 0; --kk){ //2nd to last value vkk
  64.                 dp[i][j] = min(dp[i][j], dp[kk][j - 1] + bstInBetween[kk][i]);
  65.             }
  66.              // cout << "i -> j -> " << i << " " << j << " " << dp[i][j] << endl;
  67.         }
  68.     }
  69.  
  70.     //cout << dp[n][k] << endl;
  71.     cout << dp[mx + 1][k] << endl;
  72.  
  73.     return 0;                  
  74. }
Add Comment
Please, Sign In to add comment