Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #ifdef SIR
- #define err(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
- #else
- #define err(...) 42
- #endif
- using namespace std;
- typedef long long ll;
- const int dd = 720 * 3 + 7;
- int cnt[dd];
- ll dp1[dd], dp2[dd];
- ll P[dd], S[dd];
- int n, t, g;
- struct line {
- ll k, b;
- };
- double inter(line a, line b) {
- return (a.b - b.b) * 1.0 / (b.k - a.k);
- }
- struct ConvexHull {
- vector<line> T;
- vector<double> X;
- int j = 0;
- void add(line M) {
- while (!T.empty()) {
- if (T.back().k == M.k) {
- if (T.back().b > M.b) T.pop_back(), X.pop_back();
- else return;
- } else {
- double x = inter(T.back(), M);
- if (x < X.back()) T.pop_back(), X.pop_back();
- else {
- T.push_back(M), X.push_back(x);
- return;
- }
- }
- }
- T.push_back(M);
- X.push_back(1e-18);
- }
- ll get(int i) {
- j = min(j, (int)X.size() - 1);
- while (j + 1 < (int)X.size() && X[j + 1] <= i) j++;
- return 1ll * T[j].k * i + T[j].b;
- }
- };
- ll calc(int first) {
- fill(dp1, dp1 + dd, (ll)2e18 + 1);
- fill(dp2, dp2 + dd, (ll)2e18 + 1);
- dp1[first] = first * P[first + 1] - S[first + 1];
- for (int j = 1; j < g; j++) {
- ConvexHull CH;
- for (int i = 1; i < t; i++) {
- CH.add({ -P[i], dp1[i - 1] + S[i] });
- //int cur = (int)2e9 + 1;
- /*for (int p = 1; p <= i; p++) {
- cur = min(cur, dp1[p - 1] + i * (P[i + 1] - P[p]) - (S[i + 1] - S[p]));
- }*/
- ll cur = CH.get(i) + i * P[i + 1] - S[i + 1];
- dp2[i] = min(cur, dp1[i]);
- }
- for (int i = 0; i < t; i++) {
- dp1[i] = dp2[i], dp2[i] = (int)2e9 + 1;
- }
- }
- ll ans = (ll)2e18 + 1;
- for (int i = 0; i < t; i++) {
- ans = min(ans, dp1[i] + (first + t) * (P[t] - P[i + 1]) - (S[t] - S[i + 1]));
- }
- return ans;
- }
- void solve() {
- cin >> n >> t >> g;
- t *= 2;
- for (int i = 0; i < n; i++) {
- int x; cin >> x;
- cnt[x % t]++;
- }
- for (int i = 0; i < t; i++) P[i + 1] = P[i] + cnt[i];
- for (int i = 0; i < t; i++) S[i + 1] = S[i] + i * cnt[i];
- ll ans = (ll)2e18 + 1;
- for (int i = 0; i < t; i++) {
- ans = min(ans, calc(i));
- }
- cout << ans << "\n";
- }
- int main() {
- #ifdef SIR
- freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
- #endif
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement