zoukankan      html  css  js  c++  java
  • UVA1388 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-- the number of holographic statues initially located at the ACM, and m -- the number of statues to be added (2$ \le$n$ \le$1000, 1$ \le$m$ \le$1000) . 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.

                                      

    题目大意:在一个周长为10000的园上等距分布着n个雕塑。现在又有m个雕塑加入,希望所有 m+n个雕塑在圆周上均匀分布。这就需要移动其中一些原有的雕塑。要求n个雕塑移动的总距离尽量少(直接码《训练指南》上的翻译)

    题解:从上面三个样例的示意图可以发现,有一个雕塑没有移动。即我们最多只需要移动n-1个雕塑。对于第i个需要移动的雕塑,在已确定好位置的目标雕塑中找到第一个位置比第i个雕塑位置大的目标雕塑j,那么我们只需要判断第i个雕塑移动到目标雕塑j还是j-1所需要的距离少,哪个小移动到哪个目标雕塑。

    View Code
     1 #include<stdio.h>
     2 #include<math.h>
     3 #define MAXSN 10000
     4 double min(double a,double b)
     5 {
     6     return a<b?a:b;
     7 }
     8 int main(void)
     9 {
    10     int n,m,i,j,k;
    11     double ans,ave1,ave2,s1,s2;
    12     while(scanf("%d%d",&n,&m)!=EOF)
    13     {
    14         ans=0.0;
    15         ave1=(double)MAXSN/(n+m);
    16         ave2=(double)MAXSN/n;
    17         s1=ave1;
    18         s2=ave2;
    19         for(i=1; i<n; i++)
    20         {
    21             while(s1<=s2)
    22                 s1+=ave1;
    23             ans+=min(s1-s2,s2-s1+ave1);
    24             s2+=ave2;
    25 
    26         }
    27       printf("%.4lf\n",ans);
    28 
    29     }
    30     return 0;
    31 }
  • 相关阅读:
    Python学习笔记之操作yalm
    Python学习笔记之多线程
    Python学习笔记之网络编程
    Python学习笔记之面对象与错误处理
    Python学习笔记之装饰器
    Python学习笔记之内置模块
    Python学习笔记之函数与正则
    PAT甲级1049. Counting Ones
    PAT甲级1045. Favorite Color Stripe
    PAT甲级1034. Head of a Gang
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2951588.html
Copyright © 2011-2022 走看看