mr_dot_convict

797F-Mices-and-Holes-codeforces-mr.convict

Mar 8th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.74 KB | None | 0 0
  1. Hack it and have it ;) //
  2. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  3.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  4.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  5.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  6.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  7. When I wrote this, only God and I understood what I was doing
  8.  ** * * * * * * * Now, only God knows!* * * * * * */
  9. #include      <bits/stdc++.h> /*{{{*/
  10. #include      <ext/pb_ds/assoc_container.hpp>
  11. #include      <ext/pb_ds/tree_policy.hpp>
  12. using namespace std;
  13. using namespace __gnu_pbds;
  14.  
  15. #ifndef CONVICTION
  16. #pragma GCC       optimize ("Ofast")
  17. #pragma GCC       optimize ("unroll-loops")
  18. #pragma GCC       target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  19. #endif
  20.  
  21. #define IOS       ios_base::sync_with_stdio(false); cin.tie (nullptr)
  22. #define PREC      cout.precision (10); cout << fixed
  23. #define X         first
  24. #define Y         second
  25. #define sz(x)     (int)x.size()
  26. #define fr(i,x,y) for (int i = (int)x; i <= (int)y; ++i)
  27. #define rv(i,x,y) for (int i = (int)x; i >= (int)y; --i)
  28. #define cnt(x)    __builtin_popcount(x)
  29. #define cntll(x)  __builtin_popcountll(x)
  30. #define bg(x)     " [ " << #x << " : " << (x) << " ] "
  31. #define un(x)     sort(x.begin(), x.end()), \
  32.   x.erase(unique(x.begin(), x.end()), x.end())
  33. using   ll  =     long long;
  34. using   ull =     unsigned long long;
  35. using   ff  =     long double;
  36. using   pii =     pair<int,int>;
  37. using   pil =     pair<int,ll>;
  38. using   pll =     pair<ll,ll>;
  39. using   vi  =     vector <int>;
  40. using   vl  =     vector<ll>;
  41. using   vvi =     vector <vi>;
  42. using   vvl =     vector <vl>;
  43. using   vp  =     vector <pii>;
  44. using   vvp =     vector <vp>;
  45. typedef tree
  46. < int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  47. ordered_set;
  48.  
  49. struct chash {
  50.   int operator () (pii x) const { return x.X*31 + x.Y; }
  51. };
  52. gp_hash_table <pii, int, chash> gmp;
  53.  
  54. #if __cplusplus > 201103L
  55. seed_seq seq{
  56.   (uint64_t) chrono::duration_cast<chrono::nanoseconds>
  57.     (chrono::high_resolution_clock::now().time_since_epoch()).count(),
  58.     (uint64_t) __builtin_ia32_rdtsc(),
  59.     (uint64_t) (uintptr_t) make_unique<char>().get()
  60. };
  61. mt19937 rng(seq); //uniform_int_distribution<int> (l, h)(rng); //[low, high]
  62. #else
  63. auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
  64. mt19937 rng(seed);
  65. #endif
  66.  
  67. #define debug(args...) { \
  68.   /* WARNING : do NOT compile this debug func calls with following flags: // \
  69.    * // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
  70.   string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  71.   stringstream _ss(_s); \
  72.   istream_iterator<string> _it(_ss); err(_it, args); \
  73. }
  74. void err(istream_iterator<string> it) {
  75.   it->empty(); cerr << " (Line : " << __LINE__ << ")" << '\n';
  76. }
  77. template<typename T, typename... Args>
  78. void err(istream_iterator<string> it, T a, Args... args) {
  79.   cerr << fixed << setprecision(15)
  80.     << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  81.   err(++it, args...);
  82. }
  83. //         __                                           __
  84. //        (**)                                         (**)
  85. //        IIII                                         IIII
  86. //        ####                                         ####
  87. //        HHHH     Madness comes, and madness goes     HHHH
  88. //        HHHH    An insane place, with insane moves   HHHH
  89. //        ####   Battles without, for battles within   ####
  90. //     ___IIII___        Where evil lives,          ___IIII___
  91. //  .-`_._"**"_._`-.      and evil rules         .-`_._"**"_._`-.
  92. // |/``  .`\/`.  ``\|    Breaking them up,      |/``  .`\/`.  ``\|
  93. // `     }    {     '  just breaking them in    `     }    {     '
  94. //       ) () (  Quickest way out, quickest way wins  ) () (
  95. //       ( :: )      Never disclose, never betray     ( :: )
  96. //       | :: |   Cease to speak or cease to breath   | :: |
  97. //       | )( |        And when you kill a man,       | )( |
  98. //       | || |          you're a murderer            | || |
  99. //       | || |             Kill many                 | || |
  100. //       | || |        and you're a conqueror         | || |
  101. //       | || |        Kill them all ... Ooh..        | || |
  102. //       | || |           Oh you're a God!            | || |
  103. //       ( () )                       -Megadeth       ( () )
  104. //        \  /                                         \  /
  105. //         \/                                           \/
  106. /*}}}*/
  107. /*****************************************************************************/
  108. //Don’t practice until you get it right. Practice until you can’t get it wrong
  109.  
  110. // dp(i, j) = 1...j mices in 1...i holes
  111. // dp (i, j) = min {k < j} [dp(i - 1, k) + cost(k + 1, j, i)]
  112. // also optimal k is monotonous to j for fixed i
  113. // i.e. let A(i, j) be the optimal k, then A(i, j) <= A(i, j + 1)
  114. // we can apply Divide & Conquer Optimization techinique to make this O(n^2 * m) into O(m*n*logn)
  115.  
  116. /*
  117.  * Brute force solution - That works
  118.  
  119. for (int i = 1; i <= m; ++i) {
  120.   for (int j = 1; j <= n; ++j) {
  121.     dp[i][j] = inf;
  122.     for (int k = 0; k <= j; ++k) {
  123.       if (j - (k + 1) + 1 <= holes[i].second && dp[i - 1][k] < inf)
  124.         dp[i][j] = min(dp[i][j], dp[i - 1][k] + cost(k + 1, j, i));
  125.     }
  126.   }
  127. }
  128. */
  129.  
  130. const int N = 5010, M = 5010;
  131. const long long inf = 1ll * 1e18;
  132.  
  133. int n, m;
  134. int mices[N], prefcap[M];
  135. pair <int, int> holes[M];
  136. long long dp[2][N], prefsum[N];
  137.  
  138. long long cost (int l, int r, int j) {
  139.   if (r < l) return 0;
  140.   return prefsum[r] - prefsum[l - 1];
  141. }
  142.  
  143. void dNc (int i, int l, int r, int kl, int kr) {
  144.   if (r < l) return;
  145.   int j = (l + r) / 2;
  146.   int opt = j;
  147.  
  148.   int kll = max(kl, j - holes[i].second);
  149.   int krr = min(kr, j);
  150.   for (int k = kll; k <= krr; ++k) {
  151.     if (dp[!(i & 1)][k] < inf &&
  152.         dp[i & 1][j] > dp[!(i & 1)][k] + cost(k + 1, j, i))
  153.     {
  154.       dp[i & 1][j] = dp[!(i & 1)][k] + cost(k + 1, j, i);
  155.       opt = k;
  156.     }
  157.   }
  158.  
  159.   dNc(i, l, j - 1, kl, opt);
  160.   dNc(i, j + 1, r, opt, kr);
  161. }
  162.  
  163. signed main()
  164. {
  165.   IOS; PREC;
  166.   memset(prefsum, 0, sizeof(prefsum));
  167.   memset(prefcap, 0, sizeof(prefcap));
  168.  
  169.   cin >> n >> m;
  170.   for (int i = 1; i <= n; ++i) {
  171.     cin >> mices[i];
  172.   }
  173.   for (int i = 1; i <= m; ++i) {
  174.     cin >> holes[i].first >> holes[i].second;
  175.     prefcap[i] = prefcap[i - 1] + holes[i].second;
  176.   }
  177.   sort(mices + 1, mices + n + 1);
  178.   sort(holes + 1, holes + m + 1);
  179.  
  180.   for (int i = 0; i <= n; ++i)
  181.     dp[0][i] = dp[1][i] = inf;
  182.  
  183.   dp[0][0] = 0, dp[1][0] = 0;
  184.  
  185.   for (int i = 1; i <= m; ++i)  {
  186.     for (int j = 1; j <= n; ++j) {
  187.       prefsum[j] = 1ll * abs(mices[j] - holes[i].first) + prefsum[j - 1];
  188.     }
  189.     dNc(i, 1, n, 0, n);
  190.   }
  191.  
  192.   if (dp[m & 1][n] >= inf) dp[m & 1][n] = -1;
  193.   cout << dp[m & 1][n] << '\n';
  194.  
  195.   return EXIT_SUCCESS;
  196. }
Add Comment
Please, Sign In to add comment