首页 > 代码库 > HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)

HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)

题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小。一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0。

析:DP状态方程很容易想出来,dp[i][j] 表示前 j 个数分成 i 组。但是复杂度是三次方的,肯定会超时,就要对其进行优化。

有两种方式,一种是斜率对其进行优化,是一个很简单的斜率优化

dp[i][j] = min{dp[i-1][k] - w[k] + sum[k]*sum[k] - sum[k]*sum[j]} + w[j] (i-1<=k<j)。sum[i]表示前i个数之和,w[i]表示前i个数分成一组的价值。

第二种方式就是四边形不等式进行优化,

dp[i][j] = min{dp[i-1][k] + w[k+1][j] } w[i][j] 表示第 i 个数到第 j 个数的价值和。

代码如下:

斜率优化:

#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 = 1e3 + 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][maxn], w[maxn];
int a[maxn], sum[maxn];
int q[maxn];

int getUP(int i, int j, int k){
  return dp[i-1][j] - w[j] + sum[j] * sum[j] - (dp[i-1][k] - w[k] + sum[k] * sum[k]);
}

int getDOWN(int i, int j){
  return sum[i] - sum[j];
}

int getDP(int i, int j){
  return dp[i-1][j] - w[j] + sum[j] * sum[j];
}

int main(){
  while(scanf("%d %d", &n, &m) == 2 && n+m){
    for(int i = 1; i <= n; ++i){
      scanf("%d", a+i);
      sum[i] = sum[i-1] + a[i];
      w[i] = w[i-1] + a[i] * sum[i-1];
      dp[0][i] = w[i];
    }
    for(int i = 1; i <= m; ++i){
      int fro = 0, rear = 0;
      q[++rear] = 0;
      for(int j = 1; j <= n; ++j){
        while(fro + 1 < rear && getUP(i, q[fro+2], q[fro+1]) <= sum[j]*getDOWN(q[fro+2], q[fro+1]))  ++fro;
        dp[i][j] = getDP(i, q[fro+1]) + w[j] - sum[j] * sum[q[fro+1]];
        while(fro + 1 < rear && getUP(i, j, q[rear])*getDOWN(q[rear], q[rear-1]) <= getUP(i, q[rear], q[rear-1])*getDOWN(j, q[rear]))  --rear;
        q[++rear] = j;
      }
    }
    printf("%d\n", dp[m][n]);
  }
  return 0;
}

  

四边形不等式优化:

#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 = 1e3 + 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;
}

LL dp[maxn][maxn], w[maxn][maxn];
int a[maxn], sum[maxn], s[maxn][maxn];

int main(){
  while(scanf("%d %d", &n, &m) == 2 && n+m){
    for(int i = 1; i <= n; ++i){
      scanf("%d", a+i);
      sum[i] = sum[i-1] + a[i];
    }
    for(int i = 1; i <= n; ++i){
      w[i][i] = 0;
      for(int j = i+1; j <= n; ++j)
        w[i][j] = w[i][j-1] + a[j] * (sum[j-1]-sum[i-1]);
    }

    memset(s, 0, sizeof s);
    for(int i = 0; i <= m; ++i)
      fill(dp[i], dp[i]+n+1, LNF);
    for(int i = 1; i <= n; ++i)  dp[0][i] = w[1][i];
    for(int i = 1; i <= m; ++i){
      dp[0][i] = 0;
      s[i][n+1] = n;
      for(int j = n; j >= i; --j)
        for(int k = s[i-1][j]; k <= s[i][j+1]; ++k)
          if(dp[i][j] > dp[i-1][k] + w[k+1][j]){
            dp[i][j] = dp[i-1][k] + w[k+1][j];
            s[i][j] = k;
          }
    }
    printf("%I64d\n", dp[m][n]);

  }
  return 0;
}

  

HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)