Dang_Quan_10_Tin

PROGRESS

Nov 1st, 2021 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #define task "PROGRESS"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <functional>
  6.  
  7. using namespace std;
  8.  
  9. using ll = long long;
  10. using ld = long double;
  11.  
  12. constexpr int N = 1e5 + 5;
  13. constexpr ll Inf = 1e17;
  14. struct Point
  15. {
  16.     ll x, y;
  17.     Point(ll x = 0, ll y = 0) : x(x), y(y) {}
  18.     Point operator-(const Point &a) const
  19.     {
  20.         return Point(x - a.x, y - a.y);
  21.     }
  22.     ll operator*(const Point &a)
  23.     {
  24.         return x * a.y - y * a.x;
  25.     }
  26.     bool operator<(const Point &a)
  27.     {
  28.         return x < a.x || (x == a.x && y < a.y);
  29.     }
  30. } a[N], b[N], pos[N];
  31.  
  32. int n, m, id[N];
  33. ll sum(0);
  34.  
  35. void Read()
  36. {
  37.     cin >> n;
  38.     if (n == 1)
  39.     {
  40.         cout << 0;
  41.         exit(0);
  42.     }
  43.     for (int i = 1; i <= n; ++i)
  44.     {
  45.         cin >> a[i].y;
  46.         a[i].x = i - 1;
  47.         sum += a[i].y;
  48.     }
  49. }
  50. /// Tìm tiếp điểm
  51. int Get(ll v)
  52. {
  53.     Point tmp = Point(1, v);
  54.     int l = 1, mid, h = m;
  55.  
  56.     while (l <= h)
  57.     {
  58.         mid = (l + h) / 2;
  59.         if ((b[mid + 1] - b[mid]) * tmp < 0)
  60.             l = mid + 1;
  61.         else
  62.             h = mid - 1;
  63.     }
  64.  
  65.     return l;
  66. }
  67. /// Chặt tam phân, tìm hệ số góc
  68. ll Cal(ll l, ll h, int j)
  69. {
  70.     function<ll(ll)> f = [&](ll v)
  71.     {
  72.         return 1ll * n * (n - 1) / 2 * v - sum + (b[j].y - v * b[j].x) * n;
  73.     };
  74.  
  75.     --h;
  76.     while (l <= h)
  77.     {
  78.         ll mid = (l + h) / 2;
  79.  
  80.         if (f(mid + 1) < f(mid))
  81.             l = mid + 1;
  82.         else
  83.             h = mid - 1;
  84.     }
  85.  
  86.     return f(l);
  87. }
  88.  
  89. void Solve()
  90. {
  91.     ll ans(Inf);
  92.  
  93.     for (int i = 1; i <= n; ++i)
  94.     {
  95.         while (m > 1 && (b[m - 1] - a[i]) * (b[m] - a[i]) >= 0)
  96.             --m;
  97.         b[++m] = a[i];
  98.     }
  99.  
  100.     b[m + 1] = Point(b[m].x, 0);
  101.  
  102.     for (ll x = 1e9; x >= -1e9;)
  103.     {
  104.         int j = Get(x);
  105.  
  106.         ll l = -1e9, mid, h = x;
  107.         while (l <= h)
  108.         {
  109.             mid = (l + h) / 2;
  110.             if (Get(mid) == j)
  111.                 h = mid - 1;
  112.             else
  113.                 l = mid + 1;
  114.         }
  115.  
  116.         ans = min(ans, Cal(l, x, j));
  117.  
  118.         x = h;
  119.     }
  120.  
  121.     cout << ans;
  122. }
  123.  
  124. int32_t main()
  125. {
  126.     ios::sync_with_stdio(0);
  127.     cin.tie(0);
  128.     cout.tie(0);
  129.     if (fopen(task ".INP", "r"))
  130.     {
  131.         freopen(task ".INP", "r", stdin);
  132.         freopen(task ".OUT", "w", stdout);
  133.     }
  134.     Read();
  135.     Solve();
  136. }
  137. /*
  138. 4
  139. 1 5 5 6
  140. 9
  141. 9 8 7 6 5 4 3 2 2
  142. */
  143.  
Add Comment
Please, Sign In to add comment