zoukankan      html  css  js  c++  java
  • Poj 1160 Post Office

    Post Office
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 23668   Accepted: 12705

    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

    题目大意:在v个村庄中选p个建立邮局,求所有村庄到最近邮局距离和最小值。


    思路:这道题是直接搜四边形不等式的题搜出来的,题目读完之后估了一下复杂度感觉可能是O(n^4)优化到O(n^3),但是实际上手过程中感觉处理最小距离和很浪费时间,然后又会想起以前高中做过的一道叫快餐店的题,感觉这题更像是分段dp的感觉,从起点分段处理下来即可,而且最近邮局的处理也具有一定的单调性。
    确定下新的思路后还有一个问题,那就是每次新增一个点之后,肯定会让前面这段的最优点往右移,但是不知道具体要移多少(这里其实zz了,实际上很好处理)

    然后百思不得其解就找了一篇题解看了一下,发现其实结论很简单,设dis[i][j]表示i到j之间建立一个邮局的最小距离和,那么dis[i][j]=dis[i][j-1]+a[j]-a[(i+j)/2]。因为只放一个邮局的话,最优点肯定在中位数的点上(就是这点zz了想了好久)而在中位数点上由于两边点的个数相同,所以往右偏移的时候只要没有超过下一个点,两边加减相同,所以直接加上a[j]-a[(i+j)/2]即可。
    又因为中位数点的结论,肯定不会超过下一个点,所以这个式子成立。 (奇数情况比较特殊,可以举个例子尝试一下也是成立的)

    那么处理出了这个东西就可以做一个最基础的分段dp,dp[i][j]表示到第j个村庄取了i个邮局的最小距离和,dp[i][j]=min(dp[i][j],dp[i-1][k]+dp[k+1][j])。

    下附代码:

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 using namespace std;
     5 const int INF=0X3f3f3f3f;
     6 int v,p,a[305],dp[35][305],cost[305][305];
     7 int main(){
     8     scanf("%d%d",&v,&p);
     9     for (int i=1; i<=v; i++){
    10         scanf("%d",&a[i]);
    11     }
    12     memset(cost,0,sizeof(cost));
    13     for (int i=1; i<v; i++){
    14         for (int j=i+1; j<=v; j++){
    15             cost[i][j]=cost[i][j-1]+a[j]-a[(i+j)/2];
    16         }
    17     }
    18     memset(dp,INF,sizeof(dp));
    19     for (int i=1; i<=v; i++)
    20         dp[1][i]=cost[1][i];
    21     for (int i=2; i<=p; i++){
    22         for (int j=i; j<=v; j++){
    23             for (int k=i-1; k<=j-1; k++){
    24                 dp[i][j]=min(dp[i][j],dp[i-1][k]+cost[k+1][j]);
    25             }
    26         }
    27     }
    28     printf("%d",dp[p][v]);
    29     return 0;
    30 }
    View Code
  • 相关阅读:
    Hdfs详解
    HDFS主要特性和体系结构
    HDFS简单入门
    使用docker搭建hadoop分布式集群
    HDFS理论及安装部署
    Android开发之模拟器genymotion安装apk出现错误: Install_failed_invalid_URI
    Android开发之最火的开源框架之一Xutils2详解(摘自开源作者官方介绍详解)
    Java开发之javaEE(java2EE)的介绍,java软件工程师初步阶段知识
    拾色器,可以取出电脑屏幕的任何颜色,ui以及程序员前端等常用软件,文件很小,300K
    Android开发必有功能,更新版本提示,检测是否有新版本更新。下载完成后进行安装。
  • 原文地址:https://www.cnblogs.com/i-caigou-TT/p/13768158.html
Copyright © 2011-2022 走看看