• BZOJ 1911 [APIO2010] 特别行动队


    题解

    非常显然的$O(n^2)$ 的dp转移方程 :

    $ F_i  = max( F_j +  a imes (S_i - S_j) ^2 + b imes (S_i - S_j ) + c) $ 数组S为前缀和

    进行一波分离后变成了 $ F_j + a imes (S_j)^2 - b imes S_j = 2a imes S_i imes S_j - b imes S_i - c + F_i$。

    左边的一串式子为纵坐标, $S_j$ 为横坐标, $2a imes  S_i  $当做斜率, 套上斜率优化板子就可以了

    注意维护的是上凸壳

    代码

     1 #include<cstring>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #define rd read()
     5 #define rep(i,a,b) for(int i = (a); i <= (b); ++i)
     6 #define per(i,a,b) for(int i = (a); i >= (b); --i)
     7 #define ll long long
     8 using namespace std;
     9  
    10 const int N = 1e6 + 1e5;
    11  
    12 ll f[N], a, b, c, s[N], q[N], sum[N], n;
    13  
    14 int read() {
    15     int X = 0, p = 1; char c = getchar();
    16     for(; c > '9' || c < '0'; c = getchar()) if( c == '-') p = -1;
    17     for(; c >= '0' && c <= '9'; c = getchar()) X = X * 10 + c - '0';
    18     return X * p;
    19 }
    20  
    21 double caly(int A) {
    22     return f[A] + a * sum[A] * sum[A] - b * sum[A];
    23 }
    24  
    25 double calk(int A, int B) {
    26     double ax = sum[A], bx = sum[B], ay = caly(A), by = caly(B);
    27     return (by - ay) / (bx - ax);
    28 }
    29  
    30  
    31 int main()
    32 {
    33     n = rd;
    34     a = rd; b = rd; c = rd;
    35     rep(i, 1, n) s[i] = rd, sum[i] = sum[i - 1] + s[i];
    36     int l = 1, r = 1;
    37     rep(i, 1, n) {
    38         while(l < r && calk(q[l], q[l + 1]) >= 2 * a * sum[i]) l++;
    39         ll x = sum[i] - sum[q[l]];
    40         f[i] = f[q[l]] + a * x * x + b * x + c;
    41         while(l < r && calk(q[r - 1], q[r]) <= calk(q[r], i)) r--;
    42         q[++r] = i;
    43     }
    44     printf("%lld
    ", f[n]);
    45 }
    46 
    View Code
  • 相关阅读:
    python高级函数六剑客
    测试工程师用到常用的git命令
    qing理解赋值,深浅拷贝的区别
    python设计模式之单例
    Python全栈之jQuery笔记
    畅谈python之单元测试框架-unittest
    浅析python之单元测试框架-unittest
    Python之日志处理(logging模块)
    Spring整理
    Spark学习笔记11面向对象编程
  • 原文地址:https://www.cnblogs.com/cychester/p/9502278.html
Copyright © 2020-2023  润新知