zoukankan      html  css  js  c++  java
  • HDU 3507 Print Article (斜率DP)

    题意:要输出N个数字a[N],输出的时候可以连续连续的输出,每连续输出一串,它的费用是 “这串数字和的平方加上一个常数M”。

    析:这个题很容易想到DP方程dp[i] = min{dp[j] + M + (sum[i]-sum[j])^2},但是很明显是O(n^2),TLE是必然的,所以要进行优化。

    假设 i > j > k ,并且 j 要比 k 好,那么就是 dp[j] + M + (sum[i]-sum[j])^2 < dp[k] + M + (sum[i]-sum[k])^2,

    化简 (dp[j]+sum[j]^2-(dp[k]+sum[k]^2))/(2*(sum[j]-sum[k]))<sum[i],仔细看这就是一个斜率,这就说明了 j 比 k 要好,反之 j 比 k 要差。

    然后就可以用单调队列进行优化,要删除一些不可能成为最优解的点。分别要队首和队尾进行优化。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e16;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 500000 + 10;
    const int mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    int dp[maxn], sum[maxn];
    int q[maxn];
    
    int DP(int i, int j){
      return dp[j] + m + (sum[i] - sum[j]) * (sum[i] - sum[j]);
    }
    
    int UP(int i, int j){
      return dp[i] + sum[i] * sum[i] - dp[j] - sum[j] * sum[j];
    }
    
    int DOWN(int i, int j){
      return 2 * (sum[i] - sum[j]);
    }
    
    int main(){
      while(scanf("%d %d", &n, &m) == 2){
        dp[0] = 0;
        for(int i = 1; i <= n; ++i){
          int x;
          scanf("%d", &x);
          sum[i] = x + sum[i-1];
        }
        int fro = 0, rear = 0;
        q[++rear] = 0;
        for(int i = 1; i <= n; ++i){
          while(fro + 1 < rear && UP(q[fro+2], q[fro+1]) <= sum[i] * DOWN(q[fro+2], q[fro+1]))  ++fro;
          dp[i] = DP(i, q[fro+1]);
          while(fro + 1 < rear && UP(i, q[rear]) * DOWN(q[rear], q[rear-1]) <= UP(q[rear], q[rear-1]) * DOWN(i, q[rear]))  --rear;
          q[++rear] = i;
        }
        printf("%d
    ", dp[n]);
      }
      return 0;
    }
    

      

  • 相关阅读:
    Codeforces714C【映射】
    Codeforces712C【贪心】
    Codeforces712B【= =】
    lightoj1259 【素数预处理】
    Codeforces482B【线段树构造】
    51nod 1348【next_permutation】
    hdoj5289【RMQ+二分】【未完待续】
    hdoj5875【二分+RMQ】
    RMQ算法
    AtCoder Regular Contest 061 DSnuke's Coloring
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7289640.html
Copyright © 2011-2022 走看看