zoukankan      html  css  js  c++  java
  • BZOJ 3156: 防御准备( dp + 斜率优化 )

    dp(i)表示处理完[i,n]且i是放守卫塔的最小费用.

    dp(i) = min{dp(j) + (j-i)(j-i-1)/2}+costi(i<j≤N)

    然后斜率优化

    -----------------------------------------------------------

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
     
    using namespace std;
     
    typedef long long ll;
    #define f(x) (dp[x] * 2 + ll(x) * x)
     
    const int maxn = 1000009;
     
    ll dp[maxn];
    int cost[maxn], N;
    int q[maxn], qh = 0, qt = -1;
     
    double K(int x, int y) {
    return (double) (f(x) - f(y)) / (x - y);
    }
     
    int main() {
    scanf("%d", &N);
    for(int i = 1; i <= N; i++)
    scanf("%d", cost + i);
    cost[0] = 0;
    dp[N] = cost[N];
    q[++qt] = N;
    for(int i = N; i--; ) {
    while(qt - qh > 0 && K(q[qh], q[qh + 1]) > i * 2 + 1) qh++;
    dp[i] = dp[q[qh]] + 1LL * (q[qh] - i) * (q[qh] - i - 1) / 2 + cost[i];
    while(qt - qh > 0 && K(q[qt], i) > K(q[qt - 1], q[qt])) qt--;
    q[++qt] = i;
    }
    printf("%lld ", dp[0]);
    return 0;
    }

    -----------------------------------------------------------

    3156: 防御准备

    Time Limit: 10 Sec  Memory Limit: 512 MB
    Submit: 788  Solved: 375
    [Submit][Status][Discuss]

    Description

    Input

    第一行为一个整数N表示战线的总长度。

    第二行N个整数,第i个整数表示在位置i放置守卫塔的花费Ai

    Output

    共一个整数,表示最小的战线花费值。

    Sample Input



    10
    2 3 1 5 4 5 6 3 1 2

    Sample Output


    18

    HINT



    1<=N<=10^6,1<=Ai<=10^9

    Source

  • 相关阅读:
    django路由层(一)
    计算机基础2
    Django模板语言
    Django框架介绍
    Python--格式化输出,运算符
    Python---基础
    Tensorflow--TensorflowBoard
    Tensorflow--图和会话
    Tensorflow--Tensor
    Tensorflow--基础结构
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/4985658.html
Copyright © 2011-2022 走看看