-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd.cpp
More file actions
84 lines (84 loc) · 2.82 KB
/
Copy pathd.cpp
File metadata and controls
84 lines (84 loc) · 2.82 KB
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
#pragma comment (linker, "/stack:20000000")
#pragma GCC optimize ("Ofast")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#define sz(s) (int)(s.size ())
#define all(s) s.begin (), s.end ()
#define rall(s) s.rbegin (), s.rend ()
#define Unique(s) s.resize (unique (all (s)) - s.begin ())
#define fi first
#define se second
#define endl '\n'
#define Time clock () * 1.0 / CLOCKS_PER_SEC
#define sqr(x) ((x) * 1ll * (x))
#define lcm(a, b) ((a) / gcd (a, b) * (b))
#define foreach(it, s) for (__typeof (s.begin ()) it = s.begin (); it != s.end (); ++it)
#define rep(a, b, c) for (int a = b; a <= c; ++a)
#define per(a, b, c) for (int a = b; a >= c; --a)
#define _USE_MATH_DEFINES
using namespace std;
using namespace __gnu_pbds;
template<typename T>using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T1, typename T2>using ordered_map = tree<T1, T2, less<T1>, rb_tree_tag, tree_order_statistics_node_update>;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
const double PI = (double)(acos (-1.0)), EPS = (double)(1e-7);
const int MOD = 1e9 + 7, PR = 15937, INF = 1 << 30, MXN = 3e5 + 17;
const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
template<typename T>inline bool updmin (T &a, T b) { return a > b ? a = b, 1 : 0; }
template<typename T>inline bool updmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T>inline T gcd (T a, T b) { while (a && b) a > b ? a %= b : b %= a; return a + b; }
int n, a[MXN], b[MXN], ans;
int val[] = {0, -1, 1};
set<int>s;
inline int calc (int res = 0, int cur = a[2] - a[1]) {
rep (i, 1, n - 1) {
int now = a[i + 1] - a[i];
if (now != cur) {
if (a[i + 1] + 1 - a[i] == cur) {
++a[i + 1]; ++res; continue;
}
if (a[i + 1] - 1 - a[i] == cur) {
--a[i + 1]; ++res; continue;
}
return INF;
}
}
return res;
}
int32_t main () {
ios_base::sync_with_stdio (0); cin.tie (0); cout.tie (0);
unsigned int FOR; asm ("rdtsc" : "=A" (FOR)); srand (FOR);
#ifdef _DeSeiSH_
freopen ("Input.txt", "r", stdin);
freopen ("OutputMain.txt", "w", stdout);
#else
#define HaveFreOpen 0
if (HaveFreOpen) {
#define FileName ""
freopen (FileName".in", "r", stdin);
freopen (FileName".out", "w", stdout);
}
#endif
cin >> n;
rep (i, 1, n) cin >> a[i], b[i] = a[i];
rep (i, 1, n - 1) s.insert (a[i + 1] - a[i]);
if (sz (s) == 1) return cout << 0, 0;
ans = n + n;
rep (l, 0, 2) rep (r, 0, 2) {
rep (i, 1, n) a[i] = b[i];
a[1] += val[l]; a[2] += val[r];
int add = (l > 0) + (r > 0);
ans = min (add + calc (), ans);
}
if (ans > n) return cout << -1, 0;
cout << ans;
#ifdef _DeSeiSH_
cerr << "\nMainCodeTime: " << Time << endl;
#endif
return 0;
}