Advertisement
tien_noob

K BLock - N * K instead of N * K * log

Apr 25th, 2022
852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. //Make CSP great again
  2. //Vengeance
  3. #include <bits/stdc++.h>
  4. #define TASK "TESTCODE"
  5. #define Log2(x) 31 - __builtin_clz(x)
  6. using namespace std;
  7. const int N = 1e5, K = 1e2;
  8. int dp[N + 1][K + 1], a[N + 1], n, k;
  9. stack<pair<int, int> > St;
  10. void read()
  11. {
  12.     cin >> n >> k;
  13.     for (int i = 1, j = 0; i <= n; ++ i)
  14.     {
  15.         cin >> a[i];
  16.         j = max(j, a[i]);
  17.         dp[i][1] = j;
  18.     }
  19. }
  20. void solve()
  21. {
  22.     for (int j = 2; j <= k; ++ j)
  23.     {
  24.         while(!St.empty())
  25.         {
  26.             St.pop();
  27.         }
  28.         for (int i = j; i <= n; ++ i)
  29.         {
  30.             dp[i][j] = dp[i - 1][j - 1];
  31.             while(!St.empty() && a[i] >= St.top().first)
  32.             {
  33.                 //cerr << St.top().first << ' ' << St.top().second << '\n';
  34.                 dp[i][j] = min(dp[i][j], St.top().second);
  35.                 St.pop();
  36.             }
  37.             if (St.empty() || dp[i][j] + a[i] < St.top().first + St.top().second)
  38.             {
  39.                 St.push({a[i], dp[i][j]});
  40.             }
  41.             //cerr << dp[i][j] << ' ';
  42.             dp[i][j] = St.top().first + St.top().second;
  43.         }
  44.     }
  45.     cout << dp[n][k];
  46. }
  47. int main()
  48. {
  49.     ios_base::sync_with_stdio(false);
  50.     cin.tie(nullptr);
  51.     if (fopen(TASK".INP", "r"))
  52.     {
  53.         freopen(TASK".INP", "r", stdin);
  54.         //freopen(TASK".OUT", "w", stdout);
  55.     }
  56.     int t = 1;
  57.     bool typetest = false;
  58.     if (typetest)
  59.     {
  60.         cin >> t;
  61.     }
  62.     for (int __ = 1; __ <= t; ++ __)
  63.     {
  64.         //cout << "Case " << __ << ": ";
  65.         read();
  66.         solve();
  67.     }
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement