萌新求助斜率优化样例不过
查看原帖
萌新求助斜率优化样例不过
355510
Lightwhite楼主2022/6/12 09:27
#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;
using ll = long long;
const int kN = 1e6 + 5;

ll n, a, b, c, s[kN], q[kN << 1], hd, tl, f[kN];
ll X (ll v) { return s[v]; }
ll Y (ll v) { return s[v] + a * s[v] * s[v] - b * s[v]; }
ll K (ll v) { return 2 * a * s[v]; }
ll B (ll v) { return a * s[v] * s[v] + b * s[v] + c; }
double slope (ll x, ll y) { return 1.0 * (Y (x) - Y (y)) / (X (x) - X (y)); }
int main () {
  ios :: sync_with_stdio (false);
  cin.tie (0), cout.tie (0);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i], s[i] += s[i - 1];
  }
  for (int i = 1; i <= n; i++) {
    for ( ; hd < tl && slope (q[hd], q[hd + 1]) > K (i); hd++) {
    }
    f[i] = -(K (i) * X (q[hd]) - Y (q[hd]) - B (i));
    for ( ; hd < tl && slope (q[tl - 1], q[tl]) <= slope (q[tl], i); tl--) {
    }
    q[++tl] = i;
  }
  cout << f[n] << '\n';
  return 0;
}

样例输出-11

2022/6/12 09:27
加载中...