[APIO2010]特别行动队

嘟嘟嘟


这道题dp式特别好想:

\[dp[i] = max_{j = 0} ^ {i - 1} (dp[j] + f(s[i] - s[j])) \]

其中\(f(x) = ax^ 2 + bx + c\)\(s[i] = \sum_{j = 1} ^ {i} x[j]\)
但是\(O(n ^ 2)\)过不了,需要斜率优化。
勇敢的把项拆开得到

\[dp[i] = max(dp[j] - b * s[j] + a * s[j] ^ 2 - 2a * s[i] * s[j]) + a * s[i] ^ 2 + b * s[i] + c \]

然后就是套路:把这个式子看成\(b = y - k * x\),那么
\(y = dp[j] - b * s[j] + a * s[j] ^ 2\),
\(k = 2a * s[i]\),
\(x = s[j]\),
\(b = dp[i] - a * s[i] ^ 2 - b * s[i] - c\)
方便的是\(x\)是单调递增的,\(k\)是单调递减的,于是用最朴素的单调队列维护上凸包即可。


斜率优化写的还是不太熟练

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e6 + 5;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) {last = ch; ch = getchar();}
  while(isdigit(ch)) {ans = (ans << 1) + (ans << 3) + ch - '0'; ch = getchar();}
  if(last == '-') ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}

int n, a, b, c;
ll sum[maxn], dp[maxn];
int q[maxn], l = 0, r = 0;

#define x(i) sum[i]
#define k(i) (2 * a * sum[i])
#define y(i) (dp[i] - b * sum[i] + a * sum[i] * sum[i])
db slope(int i, int j)
{
  return 1.0 * (y(i) - y(j)) / (x(i) - x(j));
}

int main()
{
  n = read();
  a = read(), b = read(), c = read();
  for(int i = 1, x; i <= n; ++i) x = read(), sum[i] = sum[i - 1] + x;
  for(int i = 1; i <= n; ++i)
    {
      while(l < r && slope(q[l], q[l + 1]) > k(i)) l++;
      dp[i] = y(q[l]) - k(i) * x(q[l]) + a * sum[i] * sum[i] + b * sum[i] + c;
      while(l < r && slope(q[r - 1], q[r]) <= slope(q[r], i)) r--;
      q[++r] = i;
    }
  write(dp[n]), enter;
  return 0;
}
posted @ 2018-12-17 11:34  mrclr  阅读(120)  评论(0编辑  收藏  举报