tsypko

F

Jul 10th, 2021 (edited)
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/rope>
  3. using namespace std;
  4. using namespace __gnu_cxx;
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef unsigned int ui;
  8. typedef long double ld;
  9. typedef pair<ll, ll> ii;
  10. typedef pair<ii, ii> iii;
  11. int MOD = 1e9 + 9;
  12. const ld E = 1e-9;
  13. #define null NULL
  14. #define ms(x) memset(x, 0, sizeof(x))
  15. #ifndef LOCAL
  16. #define endl "\n"
  17. #endif
  18. #ifndef LONG_LONG_MAX
  19. #define LONG_LONG_MAX LLONG_MAX
  20. #endif
  21. #define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  22. #define _read(x) freopen(x, "r", stdin)
  23. #define _write(x) freopen(x, "w", stdout)
  24. #define files(x) _read(x ".in"); _write(x ".out")
  25. #define filesdatsol(x) _read(x ".DAT"); _write(x ".SOL")
  26. #define output _write("output.txt")
  27. #define input _read("input.txt")
  28. #define prev time_prev
  29. #ifndef M_PI
  30. #define M_PI acos(-1)
  31. #endif
  32. #define remove tipa_remove
  33. #define next tipa_next
  34. #define left tipa_left
  35. #define right tipa_right
  36. #define mod % MOD
  37. #define y1 hello_world
  38. unsigned char ccc;
  39. bool _minus = false;
  40. template<typename T>
  41. inline T sqr(T t) {
  42.     return (t * t);
  43. }
  44. inline void read(ll &n) {
  45.     n = 0;
  46.     _minus = false;
  47.     while (true) {
  48.         ccc = getchar();
  49.         if (ccc == ' ' || ccc == '\n')
  50.             break;
  51.         if (ccc == '-') {
  52.             _minus = true;
  53.             continue;
  54.         }
  55.         n = n * 10 + ccc - '0';
  56.     }
  57.     if (_minus)
  58.         n *= -1;
  59. }
  60. inline bool read(int &n) {
  61.     n = 0;
  62.     _minus = false;
  63.     while (true) {
  64.         ccc = getchar();
  65.         if (ccc == ' ' || ccc == '\n') {
  66.             if (ccc == '\n')
  67.                 return true;
  68.             break;
  69.         }
  70.         if (ccc == '-') {
  71.             _minus = true;
  72.             continue;
  73.         }
  74.         n = n * 10 + ccc - '0';
  75.     }
  76.     if (_minus)
  77.         n *= -1;
  78.     return false;
  79. }
  80. char wwww[19];
  81. int kkkk;
  82. inline void write(ll y) {
  83.     long long x = y;
  84.     kkkk = 0;
  85.     if (x < 0) {
  86.         putchar('-');
  87.         x *= -1;
  88.     }
  89.     if (!x)
  90.         ++kkkk, wwww[kkkk] = '0';
  91.     else
  92.         while (x) {
  93.             ++kkkk;
  94.             wwww[kkkk] = char(x % 10 + '0');
  95.             x /= 10;
  96.         }
  97.     for (int i = kkkk; i >= 1; --i)
  98.         putchar(wwww[i]);
  99. }
  100.  
  101. #ifdef LOCAL
  102. #define DEBUG
  103. #endif
  104.  
  105. #ifdef DEBUG
  106. #define dbg if(1)
  107. #else
  108. #define dbg if(0)
  109. #endif
  110.  
  111. const int MAX = 505;
  112.  
  113. int dp[MAX][MAX];
  114. int c[MAX][MAX];
  115. int ar[MAX];
  116.  
  117. int solve(int l, int r){
  118.     if(l + 2 == r){
  119.         c[l][r] = l + 1;
  120.         dp[l][r] = ar[r] - ar[l];
  121.         return ar[r] - ar[l];
  122.     }
  123.     if(l + 1 >= r){
  124.         c[l][r] = l;
  125.         return 0;
  126.     }
  127.     if(dp[l][r])
  128.         return dp[l][r];
  129.     solve(l + 1, r);
  130.     solve(l, r - 1);
  131.     int ans = INT_MAX;
  132.     for(int i = c[l][r - 1]; i <= c[l + 1][r]; i++){
  133.         int res = solve(l, i) + solve(i, r);
  134.         if(res < ans){
  135.             ans = res;
  136.             c[l][r] = i;
  137.         }
  138.     }
  139.     ans += ar[r] - ar[l];
  140.     return dp[l][r] = ans;
  141. }
  142.  
  143. int main() {
  144.     sync;
  145.     srand(time(NULL));
  146.     cout.precision(10);
  147.     cout << fixed;
  148. #ifdef LOCAL
  149.     input;
  150. #else
  151.  
  152. #endif
  153.  
  154.     int t;
  155.     cin >> t;
  156.     while(t--){
  157.         int len;
  158.         int n;
  159.         cin >> len >> n;
  160.         for(int i = 1; i <= n; i++){
  161.             cin >> ar[i];
  162.         }
  163.         ms(c);
  164.         ar[n + 1] = len;
  165.         ms(dp);
  166.         cout << solve(0, n + 1) << endl;
  167.     }
  168.  
  169. }
  170.  
Add Comment
Please, Sign In to add comment