1. 程式人生 > >平行四邊形不等式優化DP——筆記

平行四邊形不等式優化DP——筆記

推很久推出方程後發現時間複雜度太大

TLE是不是很沮喪

優化方式及限定條件

來道例題

Post Office

There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each village is identified with a single integer coordinate. There are no two villages in the same position. The distance between two positions is the absolute value of the difference of their integer coordinates.

Post offices will be built in some, but not necessarily all of the villages. A village and the post office in it have the same position. For building the post offices, their positions should be chosen so that the total sum of all distances between each village and its nearest post office is minimum.

You are to write a program which, given the positions of the villages and the number of post offices, computes the least possible sum of all distances between each village and its nearest post office.

Input

Your program is to read from standard input. The first line contains two integers: the first is the number of villages V, 1 <= V <= 300, and the second is the number of post offices P, 1 <= P <= 30, P <= V. The second line contains V integers in increasing order. These V integers are the positions of the villages. For each position X it holds that 1 <= X <= 10000.

Output

The first line contains one integer S, which is the sum of all distances between each village and its nearest post office.

Sample Input

10 5
1 2 3 6 7 9 11 22 44 50

Sample Output

9

然後,題意自己百度

#include <cstdio>
#include <cstring>

using namespace std;
const int MAXN = 301;
int x[MAXN],pre[MAXN],suf[MAXN];
int w[MAXN][MAXN],dp[MAXN][MAXN],s[MAXN][MAXN];
template<typename T>
inline T Min(T a,T b) {if(a < b) return a;return b;}
int main()
{
    int n,p,i,j,k;
    scanf("%d%d",&n,&p);
    for(i = 1;i <= n;i++)
    {
        scanf("%d",&x[i]);
        pre[i] = pre[i - 1] + (i - 1) * (x[i] - x[i - 1]); 
    }
    for(i = n;i >= 1;i--)
        suf[i] = suf[i + 1] + (n - i) * (x[i + 1] - x[i]);
    for(i = 1;i <= n;i++)
        for(j = i + 1;j <= n;j++)
        {
            int mid = i + j >> 1;
            w[i][j] = pre[mid] - pre[i] - (i - 1) * (x[mid] - x[i]) + suf[mid] - suf[j] - (n - j) * (x[j] - x[mid]);
            //運用字首和避免預處理都用o(n ^ 3)
        }
    memset(dp,0x7f7f7f,sizeof(dp));
    for(i = 1;i <= n;i++)
        dp[i][1] = w[1][i];
    for(i = 1;i <= n;i++)
    {
        for(j = Min(p,i);j >= 1;j--)
        {//優化後,順序特定
            if(s[i - 1][j] == 0) s[i - 1][j] = j + 1;
            //沒被記錄的要賦一個最大可能的值
            if(s[i][j + 1] == 0) s[i][j + 1] = i;
            //沒被記錄的要賦一個最小可能的值
            for(k = s[i - 1][j];k <= s[i][j + 1];k++)
                if(dp[i][j] > dp[k][j - 1] + w[k + 1][i])
                {
                    dp[i][j] = dp[k][j - 1] + w[k + 1][i];
                    s[i][j] = k;
                    //不能用min,因為s[i][j]要賦值
                }
            //四邊形不等式優化
        }
    }
    printf("%d",dp[n][p]);
    return 0;   
}

再來一道經典題目

Monkey Party

Far away from our world, there is a banana forest. And many lovely monkeys live there. One day, SDH(Song Da Hou), who is the king of banana forest, decides to hold a big party to celebrate Crazy Bananas Day. But the little monkeys don’t know each other, so as the king, SDH must do something.
Now there are n monkeys sitting in a circle, and each monkey has a making friends time. Also, each monkey has two neighbor. SDH wants to introduce them to each other, and the rules are:
1.every time, he can only introduce one monkey and one of this monkey’s neighbor.
2.if he introduce A and B, then every monkey A already knows will know every monkey B already knows, and the total time for this introducing is the sum of the making friends time of all the monkeys A and B already knows;
3.each little monkey knows himself;
In order to begin the party and eat bananas as soon as possible, SDH want to know the mininal time he needs on introducing.

Input

There is several test cases. In each case, the first line is n(1 ≤ n ≤ 1000), which is the number of monkeys. The next line contains n positive integers(less than 1000), means the making friends time(in order, the first one and the last one are neighbors). The input is end of file.

Output

For each case, you should print a line giving the mininal time SDH needs on introducing.

Sample Input

8
5 2 4 7 6 1 3 9

Sample Output

105

百度後,非常容易發現其實就是合併石子

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int MAXN = 2002;
typedef long long ll;
ll sum[MAXN],dp[MAXN][MAXN],Time[MAXN],ans;
int s[MAXN][MAXN];

template<typename T>
inline T Min(T a,T b) {if(a < b) return a;return b;}

int main()
{
    int n,i,j,k;
    while(~scanf("%d",&n))
    {
        ans = 0x3f3f3f3f;
        memset(dp,0x3f3f3f3f,sizeof(dp));
        memset(s,0,sizeof(s));
        memset(sum,0,sizeof(sum));
        for(i = 1;i <= n;i++)
        {
            scanf("%lld",&Time[i]);
            dp[i][i] = 0;
            s[i][i] = i;
            sum[i] = sum[i - 1] + Time[i];
        }
        //一個圈
        for(i = n + 1;i <= n * 2;i++)
        {
            Time[i] = Time[i - n];
            s[i][i] = i;
            dp[i][i] = 0;
            sum[i] = sum[i - 1] + Time[i];
        }
        dp[0][0] = 0;
        //雖然不知道有沒有用,但清了準沒錯
        for(i = 2;i <= n;i++)
        {//i列舉的是長度,1的情況預處理過了
            for(j = 1;j <= n * 2 - i + 1;j++)
            {
                int l = j + i - 1;
                if(s[j][l - 1] == 0) s[j][l - 1] = j;
                if(s[j + 1][l] == 0) s[j + 1][l] = l - 1;
                for(k = s[j][l - 1];k <= s[j + 1][l];k++)
                {
                    if(dp[j][l] > dp[j][k] + dp[k + 1][l] + sum[l] - sum[j - 1])
                    {
                        dp[j][l] = dp[j][k] + dp[k + 1][l] + sum[l] - sum[j - 1];
                        s[j][l] = k;
                    }
                }
                //照例平行四邊形不等式優化
            }
        }
        for(i = 1;i <= n;i++)
            ans = Min(ans,dp[i][i + n - 1]);
        printf("%lld\n",ans);
    }
}