Graveyard
来源:互联网 发布:机会成本 知乎 编辑:程序博客网 时间:2024/05/01 09:33
Description
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 placedequidistantly(等距的) 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 equidistantdisposition(布置) 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 twointeger(整数)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.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 beprecise(精确) to at least 4 digits afterdecimal point(小数点).Pictures show the first three examples. Marked circles denote original statues, empty circles denote new equidistant places, arrows denote movement plans for existing statues.
SampleInput
2 1 2 3 3 1 10 10
SampleOutput
1666.6667 1000.0 1666.6667 0.0
我们假设原来的一个雕塑不动,并把它当作坐标原点,为了简化求解,把周长缩短为n+m。这样加入新雕塑后所有雕塑的新位置就是整数了。原来雕塑的位置此时可以表示成:i*(n+m)/n 只要我们把原来的雕塑移动到离它们最近的新位置处,就可以使总的移动距离最小。而我们注意到缩小周长之后,离旧位置较近的位置就是旧位置四舍五入之后的数。如3.6四舍五入后为4,而3.6离4比3近。四舍五入可以表示为:fioor(pos+0.5)最后不要忘记将结果放大回真实大小。代码如下:#include<cstdio>#include<cmath>double N[1002]; //注意是double类型的数组 int main(){int n,m;while(scanf("%d%d",&n,&m)==2){double s=0;for(int i=1;i<n;i++){N[i]=(double)i*(n+m)/n; //强制转换成double s+=fabs(N[i]-floor(N[i]+0.5)); }printf("%.4f\n",s*10000/(n+m)); //将数放大}return 0;}
1666.6667 1000.0 1666.6667 0.0
我们假设原来的一个雕塑不动,并把它当作坐标原点,为了简化求解,把周长缩短为n+m。这样加入新雕塑后所有雕塑的新位置就是整数了。原来雕塑的位置此时可以表示成:i*(n+m)/n 只要我们把原来的雕塑移动到离它们最近的新位置处,就可以使总的移动距离最小。而我们注意到缩小周长之后,离旧位置较近的位置就是旧位置四舍五入之后的数。如3.6四舍五入后为4,而3.6离4比3近。四舍五入可以表示为:fioor(pos+0.5)最后不要忘记将结果放大回真实大小。代码如下:#include<cstdio>#include<cmath>double N[1002]; //注意是double类型的数组 int main(){int n,m;while(scanf("%d%d",&n,&m)==2){double s=0;for(int i=1;i<n;i++){N[i]=(double)i*(n+m)/n; //强制转换成double s+=fabs(N[i]-floor(N[i]+0.5)); }printf("%.4f\n",s*10000/(n+m)); //将数放大}return 0;}
- Graveyard
- Graveyard
- Graveyard
- Graveyard
- Haunted Graveyard
- 1388 - Graveyard
- LA3708:Graveyard
- uva1388 Graveyard
- uva1388 Graveyard
- Graveyard(UVa1388)
- UVa1388 - Graveyard
- LA3708-Graveyard
- uva1388 Graveyard
- 1388 - Graveyard
- Graveyard UVALive
- Graveyard UVALive
- uva 1388 - Graveyard
- POJ-3154-Graveyard
- TCP/IP 详解卷一 - UDP
- Codeforces Round #290 (Div. 2) A,B,C,D
- oracle的ROWNUM
- 有情怀的格子布局(类似锤子桌面布局源码)
- SOAP UI 创建SOAP工程进行接口测试
- Graveyard
- 错排
- exit函数与return函数(c语言)
- Squid配置之限制IP访问
- ARToolkit—让初音在你的键盘上跳舞
- geoserver连接oracle数据源卡的解决方案
- Golang编程经验总结
- hdu 3047 Zjnu Stadium【带权并查集】
- oracle存储过程游标的使用(批号分摊)