poj 1160

Post Office
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16730   Accepted: 9053

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

Sample Output

9

Source

题目描述:
在n个村庄内建立p个邮局,邮局是建立在村庄之内,也就是说在哪个村庄内建立,就和哪个村庄的位置相同。
从结果来看,就是在一条线段内,插入了一些点,如何设立状态呢?
我之前的想法是根据邮局的位置来考虑,划分最优子结构,d[i][k]表示前i个村庄内设立k个邮局,并在第i个村庄内设立邮局,
假设选取j村庄设立邮局,问题转化为在前j个村庄内设立k-1个邮局,可是j之后,i之前的村庄是距离第j个邮局最近,还是距离第i个邮局最近呢?
重新观察,一条线段内,插入了一些点,从结果来看很容易想到用块来划分阶段,一个块内有一个邮局,块内的村庄都距离它最近。
d[i][k]=min(d[j][k-1],dp[j+1][i]);dp[j+1][i]表示[j+1,i]区间内有一个邮局,并且所有村庄都与它的最近距离.
此题初始化需要想一下: 注意到p的取值范围是1<=p<=30 ,k的取值范围应该是1到P,j的取值范围应该是0到i-1,j==i-1,表示i村庄设立邮局
j==0,表示[1,i]区间内设立一个邮局. d数组初始化为inf,需要注意d[i][1]的值,d[i][1]=dp[1][i];
#include <iostream>
#include <cstdio>
#include <cstring>
#define M 11000
const int inf =1 <<20;
using namespace std;
int V,P;
int a[M];
int d[400][40],dp[400][400];

void init()
{
   memset(d,0,sizeof(d));
   memset(dp,0,sizeof(dp));
}

void solve()
{
     for(int i=1;i<=V;i++)
     for(int t=1;t<=V;t++)
     {
        int j=i+t;
        if(j>V)
        break;
        int mid=(i+j) >> 1;
        dp[i][j]=dp[i][j-1]+a[j]-a[mid];
     }


   //  printf("
");
     for(int i=1;i<=V;i++)
     for(int k=1;k<=P;k++)
     {
          d[i][k]=inf;
          for(int j=0;j<i;j++)
         {
            if(k==1)
            d[i][k]=dp[1][i];
            else
            d[i][k]=min(d[j][k-1]+dp[j+1][i],d[i][k]);
           // printf("%d ",d[i][k]);
         }
     //  printf("
");
     }
     printf("%d
",d[V][P]);
}

int main()
{
  //freopen("test.txt","r",stdin);
  // freopen("out.txt","w",sdout);
   while(~scanf("%d%d",&V,&P))
   {
     for(int i=1;i<=V;i++)
      scanf("%d",&a[i]);
      solve();
   }
    return 0;
}
原文地址:https://www.cnblogs.com/xianbin7/p/4483211.html