以hdu3480为例学会斜率优化&&四边形优化

1、斜率优化,要通过状态转移方程算出现行变化的y1 y2 x1 x2 然后斜率比较,要维护成凸的形状
2、第一次接触的就是四边形优化,不过理解不深,其实四边形优化就是记录上一次最有值转移过来的是哪个值,然后下次转移时就直接从这个位置开始,对时间有很大的优化。

3、两者比较起来后者比较好写些,第一种方法需要一丁点数学功底(没差),如果四边形熟悉的话,用四边形会更容易写出来。

斜率优化:

/*
* this code is made by LinMeiChen
* Problem:
* Type of Problem:
* Thinking:
* Feeling:
*/
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<list>
using namespace std; typedef long long lld; typedef unsigned int ud;
#define oo 0x3f3f3f3f
#define maxn 10010
#define maxm 5010
int a[maxn]; int dp[maxm][maxn]; int q[maxn], front, rear; int main() { int n, m, T;
    scanf("%d", &T); for (int cas = 1; cas <= T; cas++) {
        scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        sort(a + 1, a + 1 + n); for (int i = 1; i <= n; i++)
            dp[1][i] = (a[i] - a[1])*(a[i] - a[1]); for (int i = 2; i <= m; i++) {
            front = rear = 0;
            q[rear++] = i - 1; for (int j = i; j <= n; j++) { while (front + 1 < rear) { int k1 = q[front]; int k2 = q[front + 1]; int x1 = a[k1 + 1]; int x2 = a[k2 + 1]; int y1 = dp[i - 1][k1] + x1*x1; int y2 = dp[i - 1][k2] + x2*x2; if (y2 - y1 <= 2 * a[j] * (x2 - x1))
                        front++; else break; } int k = q[front];
                dp[i][j] = dp[i - 1][k] + (a[j] - a[k + 1])*(a[j] - a[k + 1]); while (front + 1 < rear) { int k1 = q[rear - 2]; int k2 = q[rear - 1]; int k3 = j; int x1 = a[k1 + 1]; int x2 = a[k2 + 1]; int x3 = a[k3 + 1]; int y1 = dp[i - 1][k1] + x1*x1; int y2 = dp[i - 1][k2] + x2*x2; int y3 = dp[i - 1][k3] + x3*x3; if ((y3 - y2)*(x2 - x1) <= (y2 - y1)*(x3 - x2))
                        rear--; else break; }
                q[rear++] = j; } }
        printf("Case %d: %d\n", cas, dp[m][n]); } return 0; }
四边形优化:

/*
* this code is made by LinMeiChen
* Problem:
* Type of Problem:
* Thinking:
* Feeling:
*/
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<list>
using namespace std; typedef long long lld; typedef unsigned int ud;
#define oo 0x3f3f3f3f
#define maxn 10010
#define maxm 5010
int a[maxn]; int dp[maxm][maxn]; int mark[maxm][maxn]; int main() { int n, m, T;
    scanf("%d", &T); for (int cas = 1; cas <= T; cas++) {
        scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        sort(a + 1, a + 1 + n); for (int i = 1; i <= n; i++) {
            dp[1][i] = (a[i] - a[1])*(a[i] - a[1]);
            mark[1][i] = 1; } for (int i = 2; i <= m; i++) {
            mark[i][n + 1] = n - 1; for (int j = n; j >= i; j--) {
                dp[i][j] = -1; for (int k = mark[i - 1][j]; k <= mark[i][j + 1]; k++) { int temp = (a[j] - a[k + 1])*(a[j] - a[k + 1]); if (dp[i][j] == -1 || dp[i - 1][k] + temp < dp[i][j]) {
                        dp[i][j] = dp[i - 1][k] + temp;
                        mark[i][j] = k; } } } }
        printf("Case %d: %d\n", cas, dp[m][n]); } return 0; }


你可能感兴趣的:(算法,网络)