Dang_Quan_10_Tin

Thử nghiệm Robot THT Bảng B 2021

Jul 22nd, 2020 (edited)
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <functional>
  4. #include <queue>
  5.  
  6. using namespace std;
  7. using ll = long long;
  8.  
  9. constexpr int N = 1e3 + 5;
  10. constexpr ll Inf = 1e17 + 7;
  11. int n, m, a[N];
  12. ll dp[N][N], c[N][N];
  13.  
  14. void Read()
  15. {
  16.     cin >> n >> m;
  17.  
  18.     for(int i = 1; i <= n; ++i)
  19.         cin >> a[i];
  20.  
  21. }
  22.  
  23. void DAC(int l, int r, int optl, int optr, int j) {
  24.     if(l > r) return;
  25.     if(optl == optr) {
  26.         for(; l <= r; ++l)
  27.             if(optl <= l)
  28.                 dp[l][j] = max(dp[optl - 1][j - 1], c[optl][l]);
  29.         return;
  30.     }
  31.  
  32.     int mid = (l + r) / 2, optmid = 1;
  33.  
  34.     for(int i = optl; i <= optr && i <= mid; ++i)
  35.         if(dp[mid][j] > max(dp[i - 1][j - 1], c[i][mid])) {
  36.             dp[mid][j] = max(dp[i - 1][j - 1], c[i][mid]);
  37.             optmid = i;
  38.         }
  39.  
  40.     //cout << mid << " " << optmid << "\n";
  41.  
  42.     DAC(l, mid - 1, optl, optmid, j);
  43.     DAC(mid + 1, r, optmid, optr, j);
  44. }
  45.  
  46. void Solve() {
  47.     /// Calculate c()
  48.     for(int i = 1; i <= n; ++i) {
  49.         // Hang doi uu tien
  50.         priority_queue<int> prefix;
  51.         priority_queue<int, vector<int>, greater<int>> suffix;
  52.         // Tong cac phan tu trong hai hang doi uu tien
  53.         ll s = 0, p = 0;
  54.  
  55.         for(int j = i; j; --j) {
  56.             if(prefix.empty() || a[j] <= prefix.top()) {
  57.                 prefix.emplace(a[j]);
  58.                 p += a[j];
  59.             }
  60.             else {
  61.                 suffix.emplace(a[j]);
  62.                 s += a[j];
  63.             }
  64.             // |prefix| == |suffix| + 2
  65.             if((int)prefix.size() == (int)suffix.size() + 2) {
  66.                 p -= prefix.top();
  67.                 s += prefix.top();
  68.                 suffix.emplace(prefix.top());
  69.                 prefix.pop();
  70.             }
  71.             // |prefix| == |suffix| - 1
  72.             else if((int)prefix.size() == (int)suffix.size() - 1) {
  73.                 s -= suffix.top();
  74.                 p += suffix.top();
  75.                 prefix.emplace(suffix.top());
  76.                 suffix.pop();
  77.             }
  78.  
  79.             int x = prefix.top();
  80.             c[j][i] = (1ll * x * prefix.size() - p) + (s - 1ll * suffix.size() * x);
  81.         }
  82.     }
  83.  
  84.     ///Calculate dp
  85.     fill_n(&dp[0][0], N * N, Inf);
  86.     dp[0][0] = 0;
  87.  
  88.     for(int j = 1; j <= m; ++j)
  89.         DAC(1, n, 1, n, j);
  90.  
  91.     cout << dp[n][m];
  92. }
  93.  
  94. int main ()
  95. {
  96.     ios_base::sync_with_stdio(false);
  97.     cin.tie(NULL);
  98.     cout.tie(NULL);
  99.  
  100.     Read();
  101.     Solve();
  102. }
  103.  
Add Comment
Please, Sign In to add comment