题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829
题目大意:给定一个长度为n的序列,至多将序列分成m段,每段序列都有权值,权值为序列内两个数两两相乘之和。m<=n<=1000.
解题思路:经典的DP优化题,可以用四边形不等式优化也可以用斜率优化,我三种方法实现,两种斜率优化,一种四边形不等式,其中复杂度都为n*m,但是常熟略有差异。
状态转移方程很好想,dp[i][j] = min(dp[i][j],dp[k][j-1]+cost[k+1][j])(1<=k<i),这种方程普通写法是n*n*m,当n为1000时运算量为10亿级别,必须优化。
第一种:四边形不等式优化,这种方法是最简单的,主要是减少枚举k的次数。cost[i][j]是某段区间的权值,当区间变大,权值也随之变大,区间变小,权值也随之变小,此时就可以用四边形不等式优化。
我们设s[i][j]为dp[i][j]的前导状态,即dp[i][j] = dp[s[i][j][j-1] + cost[s[i][j]+1][j].之后我们枚举k的时候只要枚举s[i][j-1]<=k<=s[i+1][j],此时j必须从小到大遍历,i必须从大到小。
用这种方法我的代码跑了140ms。
第二种:斜率优化.其实是借鉴大牛大思路,Here,我只是抛砖引玉而已。这种方法的dp和suma数组必须为64位整数,因为平方和会超过32位整数。
用这种方法我的代码跑了350ms。
第三种:斜率优化.其实是借鉴大牛大思路,Here,我只是抛砖引玉而已。其实这题可以作为模板题,斜率优化大抵如此吧。
用这种方法我的代码跑了109ms。
测试数据:
Input:
C艹代码:
//四边形不等式 #include <stdio.h> #include <string.h> #define MAX 1100 #define INF (1<<30) int n,m,sum[MAX],cost[MAX][MAX]; int arr[MAX],dp[MAX][MAX],s[MAX][MAX]; void Initial() { int i, j, k; for (i = 1; i <= n; ++i) for (j = 1; j <= n; ++j) if (j < i) cost[i][j] = 0; else cost[i][j] = cost[i][j - 1] + arr[j] * (sum[j - 1] - sum[i - 1]); for (i = 0; i <= n; ++i) { dp[i][0] = cost[1][i]; s[i][0] = 0,s[n+1][i] = n; } } int Solve_DP() { int i,j,k; for (j = 1; j <= m; ++j) for (i = n; i >= 1; --i) { dp[i][j] = INF; for (k = s[i][j-1] ; k <= s[i+1][j]; ++k) if (dp[k][j-1] + cost[k+1][i] < dp[i][j]) { s[i][j] = k; dp[i][j] = dp[k][j-1] + cost[k+1][i]; } } return dp[n][m]; } int main() { int i,j,k; while (scanf("%d%d",&n,&m),n+m) { for (i = 1; i <= n; ++i) scanf("%d",&arr[i]),sum[i] = arr[i] + sum[i-1]; Initial(); int ans = Solve_DP(); printf("%I64d\n",ans); } }
//sum[i] = arr[1] + .. arr[i]^2 //sum2[i] = arr[1]^2 + .. arr[i]^2; //dp[i][j] = min{dp[k][j-1] -sum[i] * sum[k] + (suma[k] - sum[k]^2)/2 + (sum[k]^2 - suma[k])/2}; //斜率优化二 #include <stdio.h> #include <string.h> #define MAX 1100 #define INF (1<<30) #define int64 __int64//long long struct point { int64 x,y; }pot[MAX]; int head,tail,qu[MAX]; int n,m,arr[MAX]; int64 sum[MAX],sum2[MAX],dp[MAX][MAX]; void Initial() { for (int i = 1; i <= n; ++i) { sum[i] = arr[i] + sum[i-1]; sum2[i] = arr[i] * arr[i] + sum2[i-1]; dp[i][0] = dp[i-1][0] + arr[i] * sum[i-1]; } } int CheckIt(point p0,point p1,point p2) { return (p0.x-p1.x) * (p0.y-p2.y) - (p0.y-p1.y) * (p0.x-p2.x) <= 0; } int NotBest(point p0,point p1,int k) { return p0.y - k * p0.x > p1.y - k * p1.x; } int Solve_DP() { int i,j,k; for (j = 1; j <= m; ++j) { head = 0,tail = 0; qu[tail] = 0; for (i = j + 1; i <= n; ++i) { pot[i].x = sum[i-1]; pot[i].y = dp[i-1][j-1] + (sum[i-1] * sum[i-1] + sum2[i-1]) / 2; while (head <= tail - 1 && CheckIt(pot[qu[tail-1]],pot[qu[tail]],pot[i])) tail--; qu[++tail] = i; while (head + 1 <= tail && NotBest(pot[qu[head]],pot[qu[head+1]],sum[i])) head++; k = qu[head]; //dp[i][j] = y - k * x + c dp[i][j] = pot[k].y - sum[i] * pot[k].x + (sum[i] * sum[i] - sum2[i]) / 2; } } return dp[n][m]; } int GetInt() { char ch = ' '; while (ch < '0' || ch > '9') ch = getchar(); int x = 0; while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0',ch = getchar(); return x; } int main() { int i,j,k; while (scanf("%d%d",&n,&m),n+m) { for (i = 1; i <= n; ++i) scanf("%d",&arr[i]),sum[i] = arr[i] + sum[i-1]; Initial(); int ans = Solve_DP(); printf("%d\n",ans); } }
//cost[k+1][i]=cost[1][i]-cost[1][k]-sum[k]*(sum[i]-sum[k]) //dp[i][j]=dp[k][j-1]+cost[1][i]-cost[1][k]-sum[k]*(sum[i]-sum[k]) // =dp[k][j-1]-cost[1][k]+sum[k]^2-sum[i]*sum[k]+cost[1][i] //斜率优化一 #include <stdio.h> #include <string.h> #define MAX 1100 #define INF (1<<30) struct point { int x,y; }pot[MAX]; int head,tail,qu[MAX]; int n,m,arr[MAX],cost[MAX]; int sum[MAX],sum2[MAX],dp[MAX][MAX]; void Initial() { for (int i = 1; i <= n; ++i) { sum[i] = arr[i] + sum[i-1]; cost[i] = cost[i-1] + arr[i] * sum[i-1]; dp[i][0] = cost[i]; } } int CheckIt(point p0,point p1,point p2) { return (p0.x-p1.x) * (p0.y-p2.y) - (p0.y-p1.y) * (p0.x-p2.x) <= 0; } int NotBest(point p0,point p1,int k) { return p0.y - k * p0.x > p1.y - k * p1.x; } int Solve_DP() { int i,j,k; for (j = 1; j <= m; ++j) { head = 0,tail = 0; qu[tail] = 0; for (i = j + 1; i <= n; ++i) { pot[i].x = sum[i-1]; pot[i].y = dp[i-1][j-1] - cost[i-1] + sum[i-1] * sum[i-1]; while (head <= tail - 1 && CheckIt(pot[qu[tail-1]],pot[qu[tail]],pot[i])) tail--; qu[++tail] = i; while (head + 1 <= tail && NotBest(pot[qu[head]],pot[qu[head+1]],sum[i])) head++; k = qu[head]; //dp[i][j] = y - k * x + c dp[i][j] = pot[k].y - sum[i] * pot[k].x + cost[i]; } } return dp[n][m]; } int main() { int i,j,k; while (scanf("%d%d",&n,&m),n+m) { for (i = 1; i <= n; ++i) scanf("%d",&arr[i]),sum[i] = arr[i] + sum[i-1]; Initial(); int ans = Solve_DP(); printf("%d\n",ans); } }
本文ZeroClock原创,但可以转载,因为我们是兄弟。