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!
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 (2n1000, 1m1000) . The length of the alley along the park perimeter is exactly 10 000 feet.
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所需要的距离少,哪个小移动到哪个目标雕塑。
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 }