-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathABC338D.cpp
59 lines (59 loc) · 1.65 KB
/
ABC338D.cpp
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
#include <bits/extc++.h>
using namespace std;
namespace pbds = __gnu_pbds;
using ui = unsigned int;
using uli = unsigned long long int;
using li = long long int;
template <typename T> struct Difference {
vector<T> d;
template <typename InputIterator>
Difference(InputIterator begin, size_t n): d(n) {
InputIterator lst(begin++);
typename vector<T>::iterator it = d.begin();
*(it++) = *lst;
while (true) {
*it = *begin - *lst;
if (++it >= d.end()) break;
lst = begin++;
}
}
Difference(size_t n): d(n) {}
void add_interval(size_t l, size_t r, T val) {
d[l] += val;
if (r < d.size()) d[r] -= val;
}
template <typename OutputIterator> void get_arr(OutputIterator output) {
T sum = 0;
typename vector<T>::iterator it = d.begin();
while (true) {
*output = (sum += *it);
if (++it >= d.end()) break;
++output;
}
}
};
int main(void) {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
size_t n, m;
cin >> n >> m;
Difference<li> d(n);
vector<uli> g(m);
for (uli& i : g) cin >> i, --i;
uli bv = 0;
for (vector<uli>::const_iterator it = g.cbegin(), jt = next(it);
jt < g.cend(); ++it, ++jt) {
uli x = min(*it, *jt), y = max(*it, *jt);
if (y - x <= n / 2)
d.add_interval(x, y, n - (y - x) - (y - x)), bv += y - x;
else
d.add_interval(y, n, n - (n - (y - x)) - (n - (y - x))),
d.add_interval(0, x, n - (n - (y - x)) - (n - (y - x))),
bv += n - (y - x);
}
vector<uli> yd(n);
d.get_arr(yd.begin());
uli minyd = numeric_limits<uli>::max();
for (uli i : yd) minyd = min(minyd, i);
cout << (bv += minyd);
return 0;
}