zoukankan      html  css  js  c++  java
  • LA 3708 Graveyard(推理 参考系 中位数)

    Graveyard

    Programming contests became so popular in the year 2397 that the governor of New Earck -- the largest human-inhabited planet of the galaxy -- opened a special Alley of Contestant Memories (ACM) at the local graveyard. The ACM encircles a green park, and holds the holographic statues of famous contestants placed equidistantly along the park perimeter. The alley has to be renewed from time to time when a new group of memorials arrives.

    When new memorials are added, the exact place for each can be selected arbitrarily along the ACM, but the equidistant disposition must be maintained by moving some of the old statues along the alley.

    Surprisingly, humans are still quite superstitious in 24th century: the graveyard keepers believe the holograms are holding dead people souls, and thus always try to renew the ACM with minimal possible movements of existing statues (besides, the holographic equipment is very heavy). Statues are moved along the park perimeter. Your work is to find a renewal plan which minimizes the sum of travel distances of all statues. Installation of a new hologram adds no distance penalty, so choose the places for newcomers wisely!

    Input 

    The input file contains several test cases, each of them consists of a a line that contains two integer numbers: n <tex2html_verbatim_mark>-- the number of holographic statues initially located at the ACM, and m <tex2html_verbatim_mark>-- the number of statues to be added (2$ le$n$ le$1000, 1$ le$m$ le$1000) <tex2html_verbatim_mark>. The length of the alley along the park perimeter is exactly 10 000 feet.

    Output 

    For each test case, write to the output a line with a single real number -- the minimal sum of travel distances of all statues (in feet). The answer must be precise to at least 4 digits after decimal point.

    epsfbox{p3708.eps}<tex2html_verbatim_mark>

    Pictures show the first three examples. Marked circles denote original statues, empty circles denote new equidistant places, arrows denote movement plans for existing statues.

    Sample Input 

    2 1 
    2 3 
    3 1 
    10 10

    Sample Output 

    1666.6667 
    1000.0 
    1666.6667 
    0.0

    题目大意:在一个周长为10000的圆上等距分布着n个雕塑。现在又有m个新雕塑加入(位置可以任意放),希望所有n+m个雕塑在圆上等距分布。这就需要移动一些原有的雕塑。要求n个雕塑移动的总距离尽量小。

    分析:3个样例具有一个共同点:有一个雕塑没有移动。实际上根据中位数原理,该特点在所有情况下都成立,中位数原理证明见上上篇随笔。
      为了简单起见,我们把没动的那个雕塑作为坐标原点,其他雕塑按照逆时针顺序标上到原点的距离编号(新增雕塑之后,相邻的雕塑距离等于1)。注意,这里的距离并不是真实距离,而是按比例缩小后的距离。接下来,我们把每个雕塑移动到离它最近的位置。如果没有两个雕塑移动到相同的位置,那么这样的移动一定是最优的。

    代码如下:
     1 #include<cstdio>
     2 #include<cmath>
     3 using namespace std;
     4 
     5 int main() {
     6   int n, m;
     7   while(scanf("%d%d", &n, &m) == 2) {
     8     double ans = 0.0;
     9     for(int i = 1; i < n; i++) {
    10       double pos = (double)i / n * (n+m); //计算每个需要移动的雕塑的坐标
    11       ans += fabs(pos - floor(pos+0.5)) / (n+m); //累加移动距离
    12     }
    13     printf("%.4lf
    ", ans*10000); //等比例扩大坐标
    14   }
    15   return 0;
    16 }

    注意:在代码中,坐标pos的雕塑移动到的目标位置是floor(pos+0.5),也就是pos四舍五入后的结果。这就是坐标缩小的好处。

      实际上确实没有两个雕塑移动到相同的位置,证明如下:在我们的程序中,当坐标系缩放之后,坐标为x的雕塑被移动到了x四舍五入后的位置。如果有两个坐标分别为x和y的雕塑被移动到了同一个位置,说明x和y四舍五入后的结果相同,换句话说,差距最大的情况是x=0.5,y=1.499999...。即便是这样的情况,y-x小于1,但是这是不可能的,因为新增雕塑之后,相邻的雕塑距离才等于1,之前雕塑的数目更少,距离应当更大才对。



  • 相关阅读:
    bcdedit /copy {current} /d "xxx" 报错,提示找不到系统文件
    Moving docker images location to different partition
    docker 使用save和load命令来转移image
    docker image rm ubuntu 失败
    yum国内镜像配置
    VMware下安装CentOS7 无法通过桥接模式进行联网
    docker大概理解
    windows cmd 切换磁盘
    使用Git向GitHub上上传代码
    抛砖引玉——进程和线程的理解方式
  • 原文地址:https://www.cnblogs.com/acm-bingzi/p/3199254.html
Copyright © 2011-2022 走看看