Advertisement
Galebickosikasa

Untitled

Nov 12th, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 KB | None | 0 0
  1. // #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
  2. // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx")
  3. // #pragma comment(linker, "/stack:200000000"]
  4.  
  5. #include <iostream>
  6. #include <vector>
  7. #include <cmath>
  8. #include <algorithm>
  9. #include <unordered_set>
  10. #include <unordered_map>
  11. #include <set>
  12. #include <map>
  13. #include <queue>
  14. #include <deque>
  15. #include <bitset>
  16. #include <stack>
  17. #include <random>
  18. #include <fstream>
  19. #include <sstream>
  20. #include <chrono>
  21.  
  22. #define fi first
  23. #define se second
  24. #define pb push_back
  25. #define ll long long
  26. #define ld long double
  27. #define hm unordered_map
  28. #define pii pair<int, int>
  29. #define sz(a) (int)a.size()
  30. #define all(a) a.begin(), a.end()
  31. #define cinv(v) for (auto& x: v) cin >> x
  32. #define fr(i, n) for (int i = 0; i < n; ++i)
  33. #define fl(i, l, n) for (int i = l; i < n; ++i)
  34.  
  35. #define int ll
  36.  
  37. template <typename T1, typename T2> inline bool chkmin(T1 &x, const T2 &y) {if (x > y) {x = y; return 1;} return 0;}
  38. template <typename T1, typename T2> inline bool chkmax(T1 &x, const T2 &y) {if (x < y) {x = y; return 1;} return 0;}
  39.  
  40. using namespace std;
  41.  
  42. #ifdef LOCAL
  43. #define dbg(x) cerr << #x << " : " << x << '\n'
  44. const int maxn = 20;
  45. #else
  46. #define dbg(x)
  47. const int maxn = 2e5 + 20;
  48. #endif
  49.  
  50. //tg: @galebickosikasa
  51.  
  52. ostream& operator << (ostream& out, vector<int>& v) {
  53. for (auto& x: v) out << x << ' ';
  54. return out;
  55. }
  56.  
  57. ostream& operator << (ostream& out, pii& v) {
  58. out << v.fi << ", " << v.se;
  59. return out;
  60. }
  61.  
  62. istream& operator >> (istream& in, pii& a) {
  63. in >> a.fi >> a.se;
  64. return in;
  65. }
  66.  
  67. const ll inf = (ll) 2e9;
  68. const ld pi = asin (1) * 2;
  69. const ld eps = 1e-8;
  70. const ll mod = (ll)1e9 + 7;
  71. const ll ns = 97;
  72.  
  73. mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  74.  
  75. int check1 (int st, int fn, vector<int>& a) {
  76. int i = sz (a) - 1, j = 0;
  77. int ans = 0;
  78. fr (k, inf) {
  79. ans += abs (a[i] - a[j]);
  80. --i;
  81. if (i == j) {
  82. break;
  83. }
  84. ans += abs (a[i] - a[j]);
  85. ++j;
  86. if (i == j) {
  87. break;
  88. }
  89. }
  90. return ans;
  91. }
  92.  
  93. int check2 (int st, int fn, vector<int>& a) {
  94. int i = 0, j = sz (a) - 1;
  95. int ans = 0;
  96. fr (k, inf) {
  97. ans += abs (a[i] - a[j]);
  98. ++i;
  99. if (i == j) {
  100. break;
  101. }
  102. ans += abs (a[i] - a[j]);
  103. --j;
  104. if (i == j) {
  105. break;
  106. }
  107. }
  108. return ans;
  109. }
  110.  
  111. pair<vector<vector<int>>, int> check1 (int n) {
  112. vector<vector<int>> res (n);
  113. int last = -1;
  114. int i = n - 3, j = 0;
  115. fr (k, inf) {
  116. res[i].pb (j);
  117. res[j].pb (i);
  118. --i;
  119. if (i == j) {
  120. last = i;
  121. break;
  122. }
  123. res[i].pb (j);
  124. res[j].pb (i);
  125. ++j;
  126. if (i == j) {
  127. last = j;
  128. break;
  129. }
  130. }
  131. return {res, last};
  132. }
  133.  
  134. pair<vector<vector<int>>, int> check2 (int n) {
  135. vector<vector<int>> res (n);
  136. int last = -1;
  137. int i = 0, j = n - 3;
  138. fr (k, inf) {
  139. res[i].pb (j);
  140. res[j].pb (i);
  141. ++i;
  142. if (i == j) {
  143. last = i;
  144. break;
  145. }
  146. res[i].pb (j);
  147. res[j].pb (i);
  148. --j;
  149. if (i == j) {
  150. last = j;
  151. break;
  152. }
  153. }
  154. return {res, last};
  155. }
  156.  
  157. signed main () {
  158. ios_base::sync_with_stdio(false);
  159. cin.tie(nullptr);
  160. cout.tie(nullptr);
  161. int n;
  162. cin >> n;
  163. vector<int> goo (n);
  164. cinv (goo);
  165. if (n == 1) {
  166. cout << 0 << '\n';
  167. exit (0);
  168. }
  169. if (n == 2) {
  170. cout << 1 + abs (goo[0] - goo[1]) << '\n';
  171. exit (0);
  172. }
  173. auto ptt1 = check1 (n), ptt2 = check2 (n);
  174. vector<int> a;
  175. int ans = 0;
  176. dbg (ptt1.se);
  177. dbg (ptt2.se);
  178.  
  179. fr (st, n - 1) {
  180. a.clear ();
  181. fr (i, n) {
  182. if (i == st || i == st + 1) continue;
  183. a.pb (i);
  184. }
  185. int c1 = check1 (st, st + 1, a);
  186. int c2 = check2 (st, st + 1, a);
  187. ans = max (ans, c1 + abs (st - a.back ()) + abs (st + 1 - a[ptt1.se]) + goo[st + 1] - goo[st]);
  188. ans = max (ans, c1 + abs (st + 1 - a.back ()) + abs (st - a[ptt1.se]) + goo[st] - goo[st + 1]);
  189. ans = max (ans, c2 + abs (st - a.front ()) + abs (st + 1 - a[ptt2.se]) + goo[st + 1] - goo[st]);
  190. ans = max (ans, c2 + abs (st + 1 - a.front ()) + abs (st - a[ptt2.se]) + goo[st] - goo[st + 1]);
  191. dbg (st);
  192. dbg (ans);
  193. dbg (c1);
  194. dbg (c2);
  195. fl (fn, st + 2, n) {
  196. int i = fn - 2;
  197. for (auto& x: ptt1.fi[i]) {
  198. c1 -= abs (a[i] - a[x]);
  199. }
  200. for (auto& x: ptt2.fi[i]) {
  201. c2 -= abs (a[i] - a[x]);
  202. }
  203. --a[i];
  204. for (auto& x: ptt1.fi[i]) {
  205. c1 += abs (a[i] - a[x]);
  206. }
  207. for (auto& x: ptt2.fi[i]) {
  208. c2 += abs (a[i] - a[x]);
  209. }
  210. ans = max (ans, c1 + abs (st - a.back ()) + abs (fn - a[ptt1.se]) + goo[fn] - goo[st]);
  211. ans = max (ans, c1 + abs (fn - a.back ()) + abs (st - a[ptt1.se]) + goo[st] - goo[fn]);
  212. ans = max (ans, c2 + abs (st - a.front ()) + abs (fn - a[ptt2.se]) + goo[fn] - goo[st]);
  213. ans = max (ans, c2 + abs (fn - a.front ()) + abs (st - a[ptt2.se]) + goo[st] - goo[fn]);
  214. dbg (st);
  215. dbg (fn);
  216. dbg (ans);
  217. dbg (c1);
  218. dbg (c2);
  219. dbg (a);
  220. dbg (abs (fn - a[ptt1.se]));
  221. dbg (goo[fn] - goo[st]);
  222. dbg (abs (st - a.back ()));
  223. }
  224. }
  225. cout << ans << '\n';
  226.  
  227.  
  228.  
  229.  
  230.  
  231. }
  232.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement