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 (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 beprecise(精确) to at least 4 digits afterdecimal 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.

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;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 学生既有分期办手机被骗怎么办? 京东刚买完东西第二天降价了怎么办 微信上买东西不给退货怎么办 微信买东西转红包被骗怎么办 买手机分期被骗了怎么办 苹果手机摔弯了怎么办 6s主板摔弯了怎么办 京东显示器坏了怎么办 苹果6s手机弯了怎么办 苹果手机变弯了怎么办 苹果手机屏幕摔碎了怎么办 苹果6p后壳弯曲怎么办 苹果手机后壳有点弯曲怎么办 苹果手机x弯了怎么办 苹果8手机弯了怎么办 苹果8p手机弯了怎么办? 美版iphone弯了怎么办 oppor9手机听筒声音小怎么办 苹果7摔弯了屏幕怎么办 苹果手机压弯了怎么办 京东买的电脑坏了怎么办 拼多多低价乱价怎么办 厂家不通过经销商直接发货怎么办 媳妇吵架说我全家有病怎么办 全家都不尊重媳妇怎么办 招商闪电贷有额度不通过怎么办 闪电贷页面登录不进去怎么办 91借钱极速逾期怎么办 论文没过拿不到毕业证怎么办 大学论文不过拿不到毕业证怎么办 转店被黑中介骗了钱怎么办? 被星外转铺骗了怎么办 店铺转了后悔了怎么办 商铺转让不出去怎么办? 和包券密码丢失怎么办 天猫购物卷兑换不了怎么办 淘宝新店每天只有几个访客怎么办 注册淘宝企业店铺需要审核怎么办 淘宝店铺被投诉知识产权怎么办 一般违规扣48分怎么办 金税盘处于报税期不能开票怎么办