Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #ifdef SIR
  3. #define err(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
  4. #else
  5. #define err(...) 42
  6. #endif
  7.  
  8. using namespace std;
  9.  
  10. typedef long long ll;
  11.  
  12. const int dd = 720 * 3 + 7;
  13. int cnt[dd];
  14. ll dp1[dd], dp2[dd];
  15. ll P[dd], S[dd];
  16. int n, t, g;
  17.  
  18. struct line {
  19. ll k, b;
  20. };
  21.  
  22. double inter(line a, line b) {
  23. return (a.b - b.b) * 1.0 / (b.k - a.k);
  24. }
  25.  
  26. struct ConvexHull {
  27.  
  28. vector<line> T;
  29. vector<double> X;
  30. int j = 0;
  31.  
  32. void add(line M) {
  33. while (!T.empty()) {
  34. if (T.back().k == M.k) {
  35. if (T.back().b > M.b) T.pop_back(), X.pop_back();
  36. else return;
  37. } else {
  38. double x = inter(T.back(), M);
  39. if (x < X.back()) T.pop_back(), X.pop_back();
  40. else {
  41. T.push_back(M), X.push_back(x);
  42. return;
  43. }
  44. }
  45. }
  46. T.push_back(M);
  47. X.push_back(1e-18);
  48. }
  49.  
  50. ll get(int i) {
  51. j = min(j, (int)X.size() - 1);
  52. while (j + 1 < (int)X.size() && X[j + 1] <= i) j++;
  53. return 1ll * T[j].k * i + T[j].b;
  54. }
  55. };
  56.  
  57. ll calc(int first) {
  58. fill(dp1, dp1 + dd, (ll)2e18 + 1);
  59. fill(dp2, dp2 + dd, (ll)2e18 + 1);
  60.  
  61. dp1[first] = first * P[first + 1] - S[first + 1];
  62.  
  63. for (int j = 1; j < g; j++) {
  64.  
  65. ConvexHull CH;
  66. for (int i = 1; i < t; i++) {
  67. CH.add({ -P[i], dp1[i - 1] + S[i] });
  68. //int cur = (int)2e9 + 1;
  69. /*for (int p = 1; p <= i; p++) {
  70. cur = min(cur, dp1[p - 1] + i * (P[i + 1] - P[p]) - (S[i + 1] - S[p]));
  71. }*/
  72. ll cur = CH.get(i) + i * P[i + 1] - S[i + 1];
  73. dp2[i] = min(cur, dp1[i]);
  74. }
  75.  
  76. for (int i = 0; i < t; i++) {
  77. dp1[i] = dp2[i], dp2[i] = (int)2e9 + 1;
  78. }
  79. }
  80.  
  81. ll ans = (ll)2e18 + 1;
  82. for (int i = 0; i < t; i++) {
  83. ans = min(ans, dp1[i] + (first + t) * (P[t] - P[i + 1]) - (S[t] - S[i + 1]));
  84. }
  85. return ans;
  86. }
  87.  
  88. void solve() {
  89. cin >> n >> t >> g;
  90. t *= 2;
  91.  
  92. for (int i = 0; i < n; i++) {
  93. int x; cin >> x;
  94. cnt[x % t]++;
  95. }
  96.  
  97. for (int i = 0; i < t; i++) P[i + 1] = P[i] + cnt[i];
  98. for (int i = 0; i < t; i++) S[i + 1] = S[i] + i * cnt[i];
  99.  
  100. ll ans = (ll)2e18 + 1;
  101. for (int i = 0; i < t; i++) {
  102. ans = min(ans, calc(i));
  103. }
  104. cout << ans << "\n";
  105. }
  106.  
  107. int main() {
  108. #ifdef SIR
  109. freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  110. #endif
  111. ios_base::sync_with_stdio(false);
  112. cin.tie(0);
  113. solve();
  114. return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement