zoukankan      html  css  js  c++  java
  • POJ1160 [IOI2000]Post Office

    POJ1160 [IOI2000] Post Office

     

    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

    Solution

    入门区间DP题。

    题意是给了V个村庄,要在这些村庄中选出P个建立邮局,要求的是建成了P个邮局之后,所有村庄到邮局的距离最短。求最短距离。

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    
    using namespace std;
    
    #define MAXN 310
    
    inline int read()
    {
        char ch;
        int fl=1;
        int x=0;
        do{
          ch= getchar();
          if(ch=='-')
            fl=-1;
        }while(ch<'0'||ch>'9');
        do{
            x=(x<<3)+(x<<1)+ch-'0';
            ch=getchar();
        }while(ch>='0'&&ch<='9');
        return x*fl;
    }
    
    int v,p;
    int dis[MAXN][MAXN];
    int a[MAXN];
    int dp[MAXN][MAXN];
    
    int main()
    {
        v=read(),p=read();
        memset(dp,127,sizeof(dp));
        for(int i=1;i<=v;i++) a[i]=read();
        for(int i=1;i<=v-1;i++)
            for(int j=i+1;j<=v;j++)
                for(int k=i;k<=j;k++)
                    dis[i][j]+=abs(a[k]-a[(i+j)/2]);
        for(int i=1;i<=v;i++)
            dp[i][1]=dis[1][i];
        for(int i=2;i<=p;i++)
            for(int j=i;j<=v;j++)
                for(int k=i-1;k<j;k++)
                    dp[j][i]=min(dp[j][i],dp[k][i-1]+dis[k+1][j]); 
        printf("%d
    ",dp[v][p]);
                
    } 

     此题可用四边形不等式优化。

  • 相关阅读:
    【转】为什么说面向对象编程和函数式编程都有问题
    【转】对博士学位说永别
    【转】写给支持和反对《完全用Linux工作》的人们
    【转】完全用Linux工作
    【转】人体工学
    【转】如何掌握所有的程序语言
    【转】Kotlin 和 Checked Exception
    【转】什么是现实理想主义者
    【转】经验和洞察力
    【转】C 编译器优化过程中的 Bug
  • 原文地址:https://www.cnblogs.com/wlzs1432/p/9286598.html
Copyright © 2011-2022 走看看