1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
#include <algorithm> #include <vector> #ifdef LOCAL #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> #define all(vec) vec.begin(), vec.end() #define PB push_back #define PF push_front #define EB emplace_back #define EF emplace_front #define EM emplace #define FI first #define SE second #define pct __builtin_popcountll #define ctz __builtin_ctzll #define lson(o) (o << 1) #define rson(o) (o << 1 | 1) #define SZ(a) ((long long)a.size()) #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define ROF(i, a, b) for (int i = (a); i >= (b); --i) #define debug(var) cerr << #var << ":" << var << "\n"; #define cend cerr << "\n-----------\n" #define fsp(x) fixed << setprecision(x) #define minn(vec) *min_element(vec.begin(), vec.end()) #define maxx(vec) *max_element(vec.begin(), vec.end()) #define yyy cout << "YES\n" #define nnn cout << "NO\n"
using namespace std;
using ll = long long; using ull = unsigned long long; using DB = double; using LD = long double;
using CD = complex<double>; static constexpr ll MAXN = (ll)1e6 + 10, INF = (1ll << 61) - 1; static constexpr ll mod = (ll)1e9 + 7; static constexpr double eps = 1e-8; const double pi = acos(-1.0); ll N, M, Q, X, K, T, lT, C;
void Solve() { cin >> N >> M >> C; vector<ll> A(N); FOR(i, 1, N) { cin >> A[i - 1]; } vector<ll> sa(all(A)); sort(all(sa)); sa.resize(unique(all(sa)) - sa.begin()); ll sz = SZ(sa); vector<ll> freq(SZ(sa) + 1); auto findi = [&](ll x) { ll idx = lower_bound(all(sa), x) - sa.begin(); return idx; }; auto caldis = [&](ll l, ll r) { l %= sz; if (l < 0) l += sz; l = sa[l];
r %= sz; if (r < 0) r += sz; r = sa[r];
ll dis = 0; if (r > l) { dis = r - l; } else { dis = M + (r - l); } return dis; }; FOR(i, 1, N) { freq[findi(A[i - 1])]++; }
ll up = 0; ll sum = 0; ll ans = 0; FOR(i, 0, sz - 1) { if (up <= i) { sum = 0; up = i; } FOR(j, up, 2 * sz) { sum += freq[j % sz]; if (sum >= C) { ans += caldis(i - 1, i) * sum; #ifdef LOCAL debug(sum); debug(j); debug(caldis(i - 1, i)); #endif sum -= freq[j % sz]; sum -= freq[i]; up = j;
break; } } } cout << ans << "\n"; }
signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
Solve(); return 0; }
|