LA 3708 - Graveyard 墓地雕塑

来源:互联网 发布:直通车钻展淘宝客 编辑:程序博客网 时间:2024/06/09 16:52

点击打开链接

 

3708 - Graveyard

Time limit: 3.000 seconds

 

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, andm

-- 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.

\epsfbox{p3708.eps}

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个雕塑移动的总距离最小。另其中一个雕塑不动,作为坐标原点,其他雕塑按照逆时针顺序标上到原点的距离标号。然后,我们把每个雕塑移动到离它最近的距离,如果没有两个雕塑移动到相同的位置,那么这样的移动一定是最优的。

 

#include<stdio.h>#include<math.h>int main(){    int n,m;    while(scanf("%d%d",&n,&m)!=EOF)    {        double ans=0;        for(int i=1;i<n;i++)        {            double pos=(double)i/n*(n+m);//计算每个需要移动的雕塑的坐标            ans+=fabs(pos-floor(pos+0.5))/(n+m);//pos进行四舍五入        }        printf("%.4lf\n",ans*10000);    }    return 0;}


 

 

 

 

 

 

原创粉丝点击