Post Office

Post Office

The number of x-axis, v gives a positive integer incremented coordinates \ (\ {a_i \} \) , look for p points, so that the shortest distance and the minimum point coordinates, \ (v \ Leq 300, p \ 30 Leq \) .

solution

(Note that this question proved to be too complex to be completely rigorous and ensure prove correct and, if not read, is likely to be wrong, please contact the author)

step1

The problem from the answers must be found a few consecutive points, the coordinates of the pipe section, which is the interval division model, let \ (f [i] [j] \) represents the shortest distance to the front before i j coordinate points minimum sum, \ (W [i] [j] \) is the i-th coordinate of the j-th coordinate point in a selected coordinate so that the i-th to j-th coordinate of the point and the minimum distance ( \ (S_I = \ sum_. 1} = {J ^ ia_j \) , i.e. a prefix and).

\[f[i][j]=\min_{0\leq k<j}\{f[i-1][k]+w[k+1][j]\}\]

Border: \ (F [I] [I] = 0, I = 0,1,2, .., V \)
Answer: \ (F [P] [V] \)

显然\(w[i][j]=\sum_{k=i}^j|a_k-a_m|(m=\frac{i+j}{2})=[(a_j-a_m)+...+(a_m-a_m)+...\)

\(+(a_m-a_i)]=(a_j+...+a_{m+1})-(a_m+...+a_i)+[m-i+1-(j-m)]\)

\(a_m=(s_j-s_m)-(s_m-s_{i-1})+(2m-i-j+1)a_m=s_j-2s_m+s_{i-1}+(2m-i-j+1)a_m\)

So we can achieve the maintenance w, and then transferred to, the time complexity \ (O (pv ^ 2) \)

Reference Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#define il inline
#define ri register
using namespace std;
int a[350],s[350],mid[350][350],
    dp[35][350];
template<class free>
il free Min(free a,free b);
int main(){
    int v,p;
    scanf("%d%d",&v,&p);
    for(int i(1);i<=v;++i)
        scanf("%d",&a[i]),s[i]=s[i-1]+a[i];
    for(int i(1),j,m;i<=v;++i)
        for(j=i+1;j<=v;++j)
            m=(i+j)/2,mid[i][j]=s[j]
                -s[m]-(s[m]-s[i-1])+a[m]*(2*m-i+1-j);
    memset(dp,1,sizeof(dp)),dp[0][0]=0;
    for(int i(1),j,k;i<=p;++i)
        for(dp[i][0]=0,j=1;j<=v;++j)
            for(k=0;k<j;++k)
                dp[i][j]=Min(dp[i][j],dp[i-1][k]+mid[k+1][j]);
    printf("%d",dp[p][v]);
    return 0;
}
template<class free>
il free Min(free a,free b){
    return a<b?a:b;
}

setp2

But if you have dreams, you want to optimize, binary (splitting, compression, doubling) Obviously not, we do not need maintenance intervals and, while stacks and queues NA, monotonic maintain the balance of the tree, monotone queue, dual stack for top, binary search, not a discrete line matrix equation too complex for fast optimization of power and slope, consider the quadrangle inequality, proved crucial in the quadrilateral satisfy the inequality w


Prove: w satisfies the inequality quadrangular

prove:

Now to prove \ (i <j, w [ i] [j + 1] + w [i + 1] [j] \ geq w [i] [j] + w [i + 1] [j + 1] \ )

That proved (set their median \ (m_1, m_2, M_3, M_4 \) )

$s_{j+1}-2s_{m_1}+s_{i-1}+(2m_1-i-j)a_{m_1}+s_j-2s_{m_2}+s_{i}+(2m_2-i-j)a_{m_2}\geq $
\(s_j-2s_{m_3}+s_{i-1}+(2m_3-i-j+1)a_{m_3}+s_{j+1}-2s_{m_4}+s_{i}+(2m_4-i-j-1)a_{m_4}\)

That proved

$-2s_{m_1}+(2m_1-i-j)a_{m_1}-2s_{m_2}+(2m_2-i-j)a_{m_2}\geq $
\(-2s_{m_3}+(2m_3-i-j+1)a_{m_3}-2s_{m_4}+(2m_4-i-j-1)a_{m_4}\)

已知\(m_1=\frac{i+j+1}{2},m_2=\frac{i+j+1}{2},m_3=\frac{i+j}{2},m_4=\frac{i+j+2}{2}\)

I. \ (I Tasu J \ In \) odd

There \ (M_2 m_1 = = = M_4 M_3. 1 + \) , i.e. proof

\(-2s_{m_2}+(2m_2-i-j+1)a_{m_2}\geq -2s_{m_3}+(2m_3-i-j+1)a_{m_3}\)

\(-2a_{m_2}+(2m_2-i-j+1)a_{m_2}\geq (2m_3-i-j+1)a_{m_3}\)

\((2m_2-i-j-1)a_{m_2}\geq (2m_3-i-j+1)a_{m_3}\)

\((2(m_3+1)-i-j-1)a_{m_2}\geq (2m_3-i-j+1)a_{m_3}\)

\((2m_3-i-j+1)a_{m_2}\geq (2m_3-i-j+1)a_{m_3}\)

\ (A_ {m_2} \ geq a_ {m_3} \)

Because the coordinate increment, apparently \ (M_2 A_ {} \ A_ GEQ M_3} {\) , then is proved

II. $ I + j \ in $ even

Similarly

\ (m_1 M_3 = = = m_2 m_4-1 \) , namely card

\(-2s_{m_2}+(2m_2-i-j-1)a_{m_2}\geq-2s_{m_4}+(2m_4-i-j-1)a_{m_4}\)

\((2m_2-i-j-1)a_{m_2}\geq-2a_{m_4}+(2m_4-i-j-1)a_{m_4}\)

\((2m_2-i-j-1)a_{m_2}\geq(2(m_2+1)-i-j-3)a_{m_4}\)

\((2m_2-i-j-1)a_{m_2}\geq(2m_2-i-j-1)a_{m_4}\)

\ ((I + 1-jij) m_2 a_ {} \ geq (i-1 + jij) m_4 a_ {} \)

\ (- a_ {m_2} \ geq -a_ {m_4} \)

\(a_{m_2}\leq a_{m_4}\)

Clearly established

Over the total of the elements, according to the theorem of determined inequality quadrilateral, quadrilateral easy to know w satisfies the inequality


Thus enumeration i, then a one-dimensional linear recursive decision increment theorem monotonically increasing decision point is easy to know, as long as we use triples to queue maintains monotonically, time complexity \ (O (pvlog_2 ^ v) \)

Reference Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#define il inline
#define ri register
using namespace std;
struct san{
    int l,r,p;
}T[350];
int a[350],s[350],mid[350][350],
    dp[35][350],L,R;
il int dfs(int,int,int);
template<class free>
il free Min(free a,free b);
int main(){
    int v,p;
    scanf("%d%d",&v,&p);
    for(int i(1);i<=v;++i)
        scanf("%d",&a[i]),s[i]=s[i-1]+a[i];
    for(int i(1),j,m;i<=v;++i)
        for(j=i+1;j<=v;++j)
            m=(i+j)/2,mid[i][j]=s[j]
                -s[m]-(s[m]-s[i-1])+a[m]*(2*m-i+1-j);
    memset(dp,1,sizeof(dp)),dp[0][0]=0;
    for(int i(1),j,k;i<=p;++i){
        L=R=1,T[1].l=1,T[1].r=v,T[1].p=0;
        for(dp[i][0]=0,j=1;j<=v;++j){
            dp[i][j]=dp[i-1][T[L].p]+mid[T[L].p+1][j];if(++T[L].l>T[L].r)++L;
            while(L<=R&&dp[i-1][T[R].p]+mid[T[R].p+1][T[R].l]>=
                  dp[i-1][j]+mid[j+1][T[R].l])--R;T[++R].r=v,T[R].p=j;
            if(L<R){T[R].l=dfs(R-1,j,i-1),T[R-1].r=T[R].l-1;if(T[R].l>T[R].r)--R;}
        }
    }
    printf("%d",dp[p][v]);
    return 0;
}
il int dfs(int t,int p,int i){
    int l(T[t].l),mid,r(T[t].r);
    while(l<=r){
        mid=l+r>>1;
        if(dp[i][T[t].p]+::mid[T[t].p+1][mid]<
           dp[i][p]+::mid[p+1][mid])l=mid+1;
        else r=mid-1;
    }return l;
}
template<class free>
il free Min(free a,free b){
    return a<b?a:b;
}

step3

He noted that i, j can be regarded as a period interval, since the \ (I \ Leq J \) , then since the quadrilateral w satisfies the inequality, the boundary \ (f [i] [i ] = w [i] [i] = 0 \) and w satisfies comprising incrementing


Prove: w contains incremental meet

prove:

Lets assume that adding a coordinate better than not to increase a coordinate result, then directly add a program to replace the coordinates of a coordinate program does not increase, will make the results better, contradictory, proved.


Thus two-dimensional recursive decision theorem, easy to know quadrilateral f satisfies the inequality, then a two-dimensional recursive decision increment theorem readily know (provided \ (P [l] [r ] \) of \ (f [l] [r ] \) of the optimum decision point), \ (P [L] [R & lt-. 1] \ Leq P [L] [R & lt] \ Leq P [. 1 + L] [R & lt] \) , so we can optimize to \ (O (^ n-2) \) .

Reference Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#define il inline
#define ri register
using namespace std;
int x[550],s[550],mid[550][550],
    dp[550][550],P[550][550];
template<class free>
il free Min(free,free);
int main(){
    int v,p;scanf("%d%d",&v,&p);
    memset(dp,1,sizeof(dp));
    for(int i(1),j,m;i<=v;++i){
        scanf("%d",&x[i]),s[i]=s[i-1]+x[i];
        for(j=i-1;j;--j)
            m=i+j>>1,mid[j][i]=s[i]-
                s[m]*2+s[j-1]+(2*m-j-i+1)*x[m];
        P[i][i]=i-1,dp[i][i]=0;
    }dp[0][0]=0;
    for(int i,j(1),k;j<=v;++j)
        for(i=j-1;i;--i)
            for(k=P[i][j-1];k<=P[i+1][j];++k)
                if(dp[i][j]>dp[i-1][k]+mid[k+1][j])
                    dp[i][j]=dp[i-1][k]+mid[k+1][j],P[i][j]=k;
    printf("%d",dp[p][v]);
    return 0;
}
template<class free>
il free Min(free a,free b){
    return a<b?a:b;
}

Guess you like

Origin www.cnblogs.com/a1b3c7d9/p/11183639.html