poj 1160 Post Office 四边形优化

来源:互联网 发布:淘宝网店赚钱吗 编辑:程序博客网 时间:2024/05/02 19:20


Post Office
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 18793 Accepted: 10137

Description

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 51 2 3 6 7 9 11 22 44 50

Sample Output

9

Source

IOI 2000


题意:给出N个站点,要在其中选出M个建立邮局,求所有站点到其最近邮局的距离和的最小值。

分析:

用四边形不等式去优化序列划分模型,dp[m][n]表示组数和考虑了前n个站点。因为m<=n,故可以用区间dp的方式去考虑。

此题有个比较关键的一点是考虑到序列划分模型,f[le][ri]表示[le,ri]内的站点中建一个邮局的子问题答案,考虑绝对值的几何意义。


dp[m][n]=min{dp[m-1][k]+f[k+1][n]};


下面的代码根据:

s[i,j+1]<=s[i,j]<=s[i+1,j] 。


#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>using namespace std;#define all(x) (x).begin(), (x).end()#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)#define mes(a,x,s)  memset(a,x,(s)*sizeof a[0])#define mem(a,x)  memset(a,x,sizeof a)#define ysk(x)  (1<<(x))typedef long long ll;typedef pair<int, int> pii;const int INF =0x3f3f3f3f;const int maxN= 300+10    ;int N,M;int a[maxN+10];int dp[maxN+5][maxN+5],s[maxN+5][maxN+5],f[maxN+5][maxN+5],sum[maxN+5];void pre(){    for1(i,N)  f[i][i]=0;    for(int add=1;add<N;add++)    {        for(int le=1;le+add<=N;le++)        {            int ri=le+add;            int k= (le+ri)>>1;            f[le][ri]=sum[ri]-sum[k-1]-(ri-k+1)*a[k]            +a[k]*(k-le+1)-(sum[k]-sum[le-1]) ;        }    }}void solve(){    for(int i=1;i<=N;i++)  dp[i][i]=0,s[i][i]= i-1;    for(int add=1;add<N;add++)    {        dp[1][1+add]=f[1][1+add];        s[1][1+add]= (2+add)>>1;        for(int m=2;m<=M&&m+add<=N;m++)        {            int n=m+add;            dp[m][n]=INF;            int R= (m+1<=M )?s[m+1][n]:N-1 ;            for(int k=s[m][n-1];k<=R&&k<n;k++)            {                int ret=dp[m-1][k]+f[k+1][n];                if(ret<=dp[m][n])                {                    dp[m][n]=ret;                    s[m][n]=k;                }            }        }    }    printf("%d\n",dp[M][N]);}int main(){   std::ios::sync_with_stdio(false);   while(cin>>N>>M)   {       sum[0]=0;       for1(i,N)       {           cin>>a[i];           sum[i]=sum[i-1]+a[i];       }       pre();       solve();   }   return 0;}/*5 51 2 3 6 7*/



参考别人的代码,自己修改了一点:


这一个是根据 s[i-1,j]<=s[i,j]<=s[i,j+1]来写的,左边需要证明m[i,j]满足四边形不等式,右边需要证明f[i,j]满足四边形不等

式。

s[i,j+1]<=s[i,j]<=s[i+1,j] 也需要证明m和f满足四边形不等式。究其原因是因为dp[i][j]=min{dp[i-1][k]+f[k+1][[j]}的形式决定的。



#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>using namespace std;#define all(x) (x).begin(), (x).end()#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)#define mes(a,x,s)  memset(a,x,(s)*sizeof a[0])#define mem(a,x)  memset(a,x,sizeof a)#define ysk(x)  (1<<(x))typedef long long ll;typedef pair<int, int> pii;const int INF =0x3f3f3f3f;const int maxN= 300 ;int N,M;int sum[maxN+5],a[maxN+5],dp[maxN+5][maxN+5],s[maxN+5][maxN+5],f[maxN+5][maxN+5];void pre(){    for1(i,N)  f[i][i]=0;    for(int add=1;add<N;add++)    {        for(int le=1;le+add<=N;le++)        {            int ri=le+add;            int k= (le+ri)>>1;            f[le][ri]=sum[ri]-sum[k-1]-(ri-k+1)*a[k]            +a[k]*(k-le+1)-(sum[k]-sum[le-1]) ;        }    }}void solve(){    for1(n,N)//单独处理1组的情况    {        dp[1][n]=f[1][n];        s[1][n]=0;    }    for(int m=2;m<=M;m++)//之后区间长度维持在2及以上,参考的两个区间长度均比当前区间长。只需要预先处理下s[1][]的值。    {        s[m][N+1]=N-1;        for(int n=N;n>m;n--)        {            dp[m][n]=INF;            for(int k=max(s[m-1][n],m);k<=s[m][n+1]&&k<n;k++)            {                int ret=dp[m-1][k]+f[k+1][n];                if(ret<=dp[m][n])                {                    dp[m][n]=ret;                    s[m][n]=k;                }            }        }    }    printf("%d\n",dp[M][N]);    /*    */}int main(){   std::ios::sync_with_stdio(false);   while(cin>>N>>M)   {       sum[0]=0;       for1(i,N)       {           cin>>a[i];           sum[i]=sum[i-1]+a[i];       }       pre();       solve();   }   return 0;}


0 0
原创粉丝点击