SHOW:
|
|
- or go back to the newest paste.
| 1 | - | #include <bits/stdc++.h> |
| 1 | + | #define task "LCD" |
| 2 | - | #define X first |
| 2 | + | |
| 3 | - | #define Y second |
| 3 | + | #include <iostream> |
| 4 | #include <cstdio> | |
| 5 | ||
| 6 | - | typedef long long ll; |
| 6 | + | |
| 7 | - | typedef pair<int, int> pii; |
| 7 | + | |
| 8 | - | const int N = 3e5 + 7; |
| 8 | + | using ll = long long; |
| 9 | - | pair<ll, ll> bit[N]; |
| 9 | + | using ld = long double; |
| 10 | - | int n, l, r, b[N], m; |
| 10 | + | |
| 11 | - | pii a[N]; |
| 11 | + | constexpr int N = 1e6 + 5; |
| 12 | - | ll sum , res = LLONG_MAX, f[N]; |
| 12 | + | constexpr ll Inf = 1e17; |
| 13 | - | void upd(int p, ll x, ll y, int n) |
| 13 | + | int n, m; |
| 14 | int a[N]; | |
| 15 | - | while (p<=n) |
| 15 | + | ll b[N * 2]; |
| 16 | ||
| 17 | - | bit[p].X+=x; |
| 17 | + | void Read() |
| 18 | - | bit[p].Y+=y; |
| 18 | + | |
| 19 | - | p+=(p&(-p)); |
| 19 | + | cin >> n >> m; |
| 20 | for (int i = 1; i <= n; ++i) | |
| 21 | {
| |
| 22 | - | pair<ll, ll> Get(int p) |
| 22 | + | cin >> a[i]; |
| 23 | --a[i]; /// Change 1, 2, ..., m -> 0, 1, 2, ..., (m - 1) | |
| 24 | - | ll res = 0, dem = 0; |
| 24 | + | |
| 25 | - | while (p>0) |
| 25 | + | |
| 26 | ||
| 27 | - | res+=bit[p].X; |
| 27 | + | void Solve() |
| 28 | - | dem+=bit[p].Y; |
| 28 | + | |
| 29 | - | p-=(p&(-p)); |
| 29 | + | ll sum(0); // Answer without X |
| 30 | ||
| 31 | - | return make_pair(res,dem); |
| 31 | + | for (int i = 1; i < n; ++i) |
| 32 | {
| |
| 33 | - | int main() |
| 33 | + | if (a[i] > a[i + 1]) |
| 34 | a[i + 1] += m; | |
| 35 | - | |
| 35 | + | |
| 36 | - | ios_base::sync_with_stdio(0); |
| 36 | + | sum += a[i + 1] - a[i]; |
| 37 | ||
| 38 | b[a[i] + 2] += 1; | |
| 39 | - | cin>>n>>m; |
| 39 | + | b[a[i + 1] + 1] += -1 - (a[i + 1] - a[i] - 1); |
| 40 | - | for (int i=1; i<=n; i++) cin>>b[i]; |
| 40 | + | b[a[i + 1] + 2] += (a[i + 1] - a[i] - 1); |
| 41 | - | for (int i=1; i<n; i++) |
| 41 | + | |
| 42 | - | if (b[i]<b[i+1]) a[i] = make_pair(b[i],b[i+1]); else a[i] = make_pair(b[i],b[i+1] + m); |
| 42 | + | if (a[i + 1] >= m) |
| 43 | - | n--; |
| 43 | + | a[i + 1] -= m; |
| 44 | - | sort(a+1, a + 1 + n); |
| 44 | + | |
| 45 | - | for (int i=1; i<=n; i++) sum = sum + a[i].Y - a[i].X; |
| 45 | + | |
| 46 | - | int j = 0; |
| 46 | + | for (int i = 1; i < m * 2; ++i) |
| 47 | - | for (int x = 1; x<=m; x++) |
| 47 | + | b[i] += b[i - 1]; |
| 48 | ||
| 49 | - | while (j<n && a[j+1].X<x) |
| 49 | + | for (int i = 1; i < m * 2; ++i) |
| 50 | - | {
|
| 50 | + | b[i] += b[i - 1]; |
| 51 | - | j++; |
| 51 | + | |
| 52 | - | upd(a[j].Y, a[j].X, 1, 2*m); |
| 52 | + | ll ans(Inf); |
| 53 | - | } |
| 53 | + | |
| 54 | - | pair<ll, ll> t = Get(2*m), q = Get(x-1); |
| 54 | + | for (int i = 0; i < m; ++i) |
| 55 | - | ll cnt = t.Y - q.Y; |
| 55 | + | ans = min(ans, sum - b[i] - b[i + m]); |
| 56 | - | ll sum1 = t.X- q.X; |
| 56 | + | |
| 57 | - | ll tmp = sum + sum1 - 1LL*cnt*x + cnt; |
| 57 | + | cout << ans; |
| 58 | - | f[x] = tmp; |
| 58 | + | |
| 59 | ||
| 60 | - | for (int i=1; i<=2*m; i++) bit[i] = make_pair(0,0); |
| 60 | + | int32_t main() |
| 61 | - | j = 0; |
| 61 | + | |
| 62 | - | for (int x = 1; x<=m; x++) |
| 62 | + | ios::sync_with_stdio(0); |
| 63 | cin.tie(0); | |
| 64 | - | while (j<n && a[j+1].X<x+m) |
| 64 | + | |
| 65 | - | {
|
| 65 | + | if (fopen(task ".INP", "r")) |
| 66 | - | j++; |
| 66 | + | |
| 67 | - | upd(a[j].Y, a[j].X, 1, 2*m); |
| 67 | + | freopen(task ".INP", "r", stdin); |
| 68 | - | } |
| 68 | + | freopen(task ".OUT", "w", stdout); |
| 69 | - | pair<ll, ll> t = Get(2*m), q = Get(x+m-1); |
| 69 | + | |
| 70 | - | ll cnt = t.Y - q.Y; |
| 70 | + | Read(); |
| 71 | - | ll sum1 = t.X- q.X; |
| 71 | + | Solve(); |
| 72 | - | ll tmp = f[x] + sum1 - 1LL*cnt*(x+m) + cnt; |
| 72 | + | |
| 73 | - | f[x] = tmp; |
| 73 | + | |
| 74 | - | res = min(res,f[x]); |
| 74 | + | /* |
| 75 | 4 5 | |
| 76 | - | cout<<res; |
| 76 | + | 1 2 4 5 |
| 77 | - | return 0; |
| 77 | + | */ |